2

Is possible to redirect category to first available child post please? Thanks!

1 Answer 1

2

I had to do that, but only in some categories in my site. This was the code I used - I put it in my theme's functions.php:

  // use template_redirect action:
 add_action( 'template_redirect', 'my_redirect_to_post' );

 /// Function to redirect from certain category pages to first post of those categories
 function my_redirect_to_post() {
     if (is_category('3') ||  is_category('5') ) {
         $category = get_the_category();
         $posts =  query_posts('showposts=1&cat='.$category[0]->cat_ID);
         if(have_posts()) :
            wp_redirect(get_permalink($post->ID));
         endif;
    }
}

Of course, if you need to do it in all your categories, you can remove the condition if (is_category('3') || is_category('5') ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.