0

I would like to load the last post when I go to the selected category.

For example
if I go to http://site.com/category
it loads http://site.com/category/35/this-is-the-last-post

Does exist some example to follow about that? I'm doing practice with Wordpress and I don't know how can I get the last post of a category.

1 Answer 1

1

Hook template_redirect and check if it's a category archive page, if so get the category ID from query_vars and query_posts for a single post from that category, which by default should be the most recent, then wp_redirect to the post's permalink.

This code would go in your theme's functions.php file:

function my_category_redirect() {
    if ( is_category() ) :
        query_posts('cat='. get_query_var('cat') .'&posts_per_page=1');
        wp_redirect( get_permalink() , 301 ); 
        exit; 
    endif;
}
add_action( 'template_redirect', 'my_category_redirect' );

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.