Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'pre_get_posts', 'bounce_subcat_to_parent' );
- function bounce_subcat_to_parent( $query ) {
- if ( is_admin() ) {
- return;
- }
- if ( $query->is_main_query() && $query->is_category ) {
- $cat = get_queried_object();
- //var_dump($cat);
- //exit;
- if ( 0 != $cat->category_parent ) {
- $link = get_category_link( $cat->category_parent );
- $link = str_replace( 'category/', '', $link );
- var_dump($link);
- exit;
- wp_safe_redirect( $link );
- }
- }
- }
- add_action( 'template_redirect', 'bounce_special_post_to_cat' );
- function bounce_special_post_to_cat( ) {
- global $wpdb, $wp_query;
- // Guess the current post_type based on the query vars.
- if ( get_query_var('post_type') )
- $post_type = get_query_var('post_type');
- else if ( !empty($wp_query->query_vars['pagename']) )
- $post_type = 'page';
- else
- $post_type = 'post';
- if ( is_singular() && 'post' == $post_type ) {
- global $post;
- $cats = wp_get_object_terms($post->ID, array( 'category'), $args = array( 'fields' => 'all' ) );
- //error_log( var_export( $cats, 1 ));
- foreach( $cats as $cat ) {
- // check for meta data flag
- $flat = get_terms_meta( $cat->term_id, 'flatcategory', true );
- if ( 'checked' == $flat ) {
- $link = get_category_link( $cat->term_id );
- wp_safe_redirect( $link );
- exit();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment