Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

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' );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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