Display most recent post in category instead of archive?

I’ve successfully redirected my blog page to my most recent post – i.e. so when you click the ‘blog’ link in the nav it goes to the most recent post page and you can click next/prev buttons to navigate between other posts.

Now I want to do the same thing but for a specific category so when you click a category link, instead of displaying an archive page of all the posts in that category, it displays the most recent post in that category.

Read More

The next/prev buttons then need to navigate to posts only in that category.

Any ideas how I can do this?

Thanks

===============UPDATE===============

I copied my code from single.php to category.php and used single_cat_title(); to display the category name at the top of the page.

Needed to change my next/previous_post_link to next/previous_posts_link

The only disadvantage is that the URL displays /cat=[catname]&paged=2 rather than the name of the post, but not really a big problem.

The only issue remaining is how to display the full screen background image which is attached to each post and referenced as follows in my header.php:

$background_image = get_post_meta($page_id, 'mb_background_image', true);   
$src = wp_get_attachment_image_src($background_image, 'full');

How can I call this from the category page?

===============UPDATE 2===============

Fixed by replacing this line:

$background_image = get_post_meta($post->ID, 'mb_background_image', true);          

Related posts

Leave a Reply

2 comments

  1. You know, the other way to do what you’re trying to do is simply to set the global posts_per_page variable to 1 in Dashboard -> Settings -> Reading.

  2. You can do this by creating a template in your theme called category.php using the following.

    <?php 
    $category = get_query_var('category_name');
    $category = get_term_by('name', $category, 'category');
    $category_ID = $category->term_id;
    $post = get_posts("numberposts=1&post_type=post&category=$category_ID");
    $post = $post[0];
    $url = get_permalink($post->ID);
    wp_redirect( $url, 301 ); 
    exit;
    

    You can maintain category consistency by using the following function. Make sure in_same_cat is set to true.

    <?php next_post_link('format', 'link', 'in_same_cat', 'excluded_categories'); ?> 
    

    Please see the examples here: http://codex.wordpress.org/Function_Reference/previous_post_link and http://codex.wordpress.org/Function_Reference/next_post_link

    Not sure how you want to handle the same category for the posts links. There’s really no context to say which category is the one that should be chosen. If there are common categories between posts, it will use that in the query. You can alter the query using the following. I don’t know exactly what you wish to do so I included a var_dump so you can see what’s going on behind the curtain.

    function change_adjacent_post_query($join){
        var_dump($join);
        //Alter the query here to your liking
        return $join;
    }
    add_filter('get_previous_post_join', 'change_adjacent_post_query', 1);
    add_filter('get_next_post_join', 'change_adjacent_post_query', 1);