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'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.

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);          
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
thanks Chip but i don't think that will work for me because it doesn't actually display the full post page and im using a custom metabox to display a full screen photo image background for each post. –  fxfuture Sep 26 '11 at 23:25
    
You have control over category.php; you can make it display whatever you want for it to display. –  Chip Bennett Sep 27 '11 at 0:34
    
Thanks Chip, this worked great. I've updated my post as I have one minor issue outstanding. –  fxfuture Sep 27 '11 at 3:27

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);
share|improve this answer
    
If you want to target a specific category, name the file category-CATEGORYSLUG.php –  Brian Fegter Sep 26 '11 at 6:25
    
Thanks Brian. The category.php code works great. However, if I change my next/prev post links on single.php to in_same_cat, this affects browsing through posts even when no category link has been clicked. And what happens if there are multiple categories in a post? I would also like to display text at the top of the page saying: Currently Viewing Posts in Category: [category name]. Is it possible to query whether you're viewing posts in a certain category? –  fxfuture Sep 26 '11 at 21:59
    
See my latest edit. I don't know that there's a specific way to identify one category unless you include some sort of post meta identifying which category is preeminent. –  Brian Fegter Sep 26 '11 at 22:45
2  
You could just use the category.php template file, which will already be querying for the current category... (edit: oh, wait: y'all are already on that... er, why, then, do we need to custom-query for the category?) –  Chip Bennett Sep 26 '11 at 22:50
    
Thanks - I'll give this a go now. To clarify the functionality of my site… 1) Clicking the blog nav link redirects to the most recent post, then you can click next/prev to navigate through all posts (sorted in other question thanks to you!). 2) I also have a tag cloud. So when you click a tag (category), it should do exactly the same as above but only for posts within that category, with text at the top of page letting you know you are only viewing posts in that category. –  fxfuture Sep 26 '11 at 23:02

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.