Support » Fixing WordPress » Previous/Next Post in Category?

  • I want to create a special category, let’s call it “motorcycles” just for the sake of example.

    I want to create a page that will list all posts under the “motorcycles” category on the page. I can get this far.

    However, when the user selects a post from that list to read said post, I want the “previous” and “next” post links to go to the previous/next post in the “motorcycles” category only – I want to exclude all non-motorcycles posts in the previous and next links.

    Is this easy to do and I’m just missing something?

Viewing 15 replies - 1 through 15 (of 37 total)
  • I think you’ll find the answer here.

    in_same_cat argument should be set to TRUE.

    Maybe I’m just dense, but I’m not getting this to work.

    I activated permalinks and I added the TRUE condition to my previous and next links in the single.php file. Which I guess is where it’s supposed to go? But now, of course, I can only navigate between posts in the same category. But what about my “normal” blog (no category filter)? If I go to my normal blog page, I can now only navigate to other posts within the same category as the last post.

    Do I need to set up a separate “single-post” page php file? How does WordPress find it and know when to use it?

    I think I see what you’re saying.

    You have a homepage that displays all your posts except those in moto cat. Then you’ve set up a separate page that lists only posts in category motorcycle.

    And what you want is for the previous/next post links to cycle through all post on the homepage, and only motorcycle posts on the motorcycle page. Right?

    The problem with how you’ve implemented it is that with your current setup, no matter where a post is clicked on, it uses the single.php template.

    What you could do to obtain this functionality is use conditional checks for your previous/next post link code. Something like the following:

    <?php if (in_category('motoID')){ ?>
    			<div class="alignleft"><?php previous_post_link('&laquo; %link','%title',TRUE,'') ?></div>
    			<div class="alignright"><?php next_post_link('%link &raquo;','%title',TRUE,'') ?></div>
    			<?php }
    			else { ?>
    			<div class="alignleft"><?php previous_post_link('&laquo; %link','%title','','motoID') ?></div>
    			<div class="alignright"><?php next_post_link('%link &raquo;','%title','','motoID') ?></div>
    			<?php } ?>

    Replace motoID with the motorcycle category ID. Code is untested but should work. What this does is if it’s in the moto category, it restricts the previous/next post links to the moto category. For any other category, it allows free navigation through the posts BUT excludes the moto category.

    Hopefully that gets you what you want. There’s a possibility that what you really want is for the previous/next post links to be restricted to the moto category only when a moto post is accessed through the moto-specific page you made, but NOT when a moto post is accessed from the homepage. If this is the case, the issue is a bit more difficult to resolve.

    Thanks. I had arrived at the conclusion that I would need to include conditional statements in the single.php file last night before falling asleep.

    And your last paragraph is correct – in an ideal situation, I only want the category to be restricted if the user accesses the post from the category-specific page.

    However, I’m willing to compromise at this point just to get the client off my back. 🙂

    That’s an interesting issue, then. I’m trying to brainstorm a solution.

    If you sent moto posts from the moto page to a different page template than moto posts from the homepage, you’d have duplicate content issues.

    You could try to determine the referring page and base conditional previous/next post links off of that or something.

    Maybe someone else has an idea/solution. Having trouble getting my brain to function this morning.


    I am having a very similar issue and I understand where Katana is coming from.

    I have multiple categories each with their own posts. I have set my permalinks and added the true condition. This works great for my categories. By clicking on a post in my category I can navigate back and forth through any of the other posts within that category.

    My problem lies with my homepage. This displays all my posts from any categories in chronological order. When I click into one of these posts you would expect to be able to navigate through them as you see them on the home page irrespective of their category. Unfortunately once you click on a post from the homepage you can only navigate through the category that that post was created in.

    Like Katana, this solution will have to do for now.

    I do have a quick question though regarding the IF statement that you provided above. What the correct condition be if you were not in any category (ie you are on the home page)? I would like it to look something like this:

    <?php if (ON HOME PAGE){ ?>
    			<div class="alignleft"><?php previous_post_link('&laquo; %link','%title',FALSE,'') ?></div>
    			<div class="alignright"><?php next_post_link('%link &raquo;','%title',FALSE,'') ?></div>
    			<?php }
    			else { ?>
    			<div class="alignleft"><?php previous_post_link('&laquo; %link','%title','TRUE','') ?></div>
    			<div class="alignright"><?php next_post_link('%link &raquo;','%title','TRUE','') ?></div>
    			<?php } ?>

    Thanks in advance,

    The conditional would be is_home() or is_front_page(), but it wouldn’t function right.

    By definition really, the next/previous post links are designed for a single post view, which is why the code is added to single.php. Which means you’ll always be on single.php and the is_home() conditional would never resolve to true.

    The problem you two are having is that whether you click to a post from the homepage or the same post from a different page, the template handling the single post view is single.php. There’s nothing in our solutions to track where a user clicked on the post FROM, and therefore no way to condition the previous/next post links dependent upon whether they came from homepage or your custom category page.

    Yet, this is exactly my wish too.
    The behaviour from previous-posts – next posts (yes, the other one) on the index.php and on the category.php does exactly that. index.php flips through all posts (say 5 per page) and category.php flips thrpough posts in the category you chose from the menu only.

    Now if you make a , lets say, category-10.php template, when clicking on the menu item you land on that specific category page.
    Visitors can go on from there by clucking on the title
    <h2>” rel=”bookmark” title=”Ga naar: <?php the_title(); ?>”> <?php the_title(); ?></h2>

    or on the read more link
    <?php the_content(‘.. lees meer ..’); ?>

    Now if I could tell these two links on this specific category template to not use single.php but singlewithtrue.php!!! But how?

    So single.php contains:
    <div class=”containerbottomtextalignleft”><?php previous_post_link(‘ %link’, ‘« Ouder bericht’); ?></div>
    <div class=”containerbottomtextalignright”><?php next_post_link(‘ %link’, ‘Nieuwer bericht »’); ?></div>

    and singlewithtrue contains:
    <div class=”containerbottomtextalignleft”><?php previous_post_link(‘ %link’, ‘« Ouder bericht’, TRUE); ?></div>
    <div class=”containerbottomtextalignright”><?php next_post_link(‘ %link’, ‘Nieuwer bericht »’, TRUE); ?></div>

    then I would be the man.


    My site is live now
    but I still have not found a solution for the above mentioned problem?



    Same request here too!
    I would love to be able to do exactly the same thing: different of ways of navigating through posts depending of where the user comes from… Is there a way to do that???



    henkholland and iconico:

    Take a look at this:

    You would turn your single.php into a conditional splitter that assigned all your posts to your custom single pages by category. So your single.php file would only contain:

    <?php $post = $wp_query->post;
    if ( in_category('31') ) {include(TEMPLATEPATH . '/single-custom1.php');}
    elseif ( in_category('19') ) {include(TEMPLATEPATH . '/single-custom2.php');}
    elseif ( in_category('172') ) {include(TEMPLATEPATH . '/single-custom3.php');}
    else {include(TEMPLATEPATH . '/single1.php');}

    and then single-custom1.php (name it whatever you want of course) would call in posts from just that category so you could modify your previous/next code as you wish.

    The last bit, else {include…} single1.php means that any post that didn’t have a specific category would get the default single post template, here named single1.php but the name could be anything.

    Hope that helps!



    Thank you Smallina. I found and tried that route before. But still, it checks wether a post is “in” a certain category and acts accordingly. It does not check where the reader is coming from.
    To explain once more, here is my test site:

    Now, when a new visitor lands there after, say, three days, he sees 10 recent posts on the front page (four without scrolling). Lets say you quickly read the titles of these four and think: hey, nr 2 and 3 are interesting. You click on the title of nr 2, read it and then see the nextpost link. You click it and expect nr 3. So far no problem if I make a nextpost link without TRUE.
    But, imagine the next visitor; he is a regular and goes straight for his favorite category in the left menu. Try “uitslagen en standen” ; he clicks the top one, reads it and also uses “older post”. The he gets a post from a different category (not what he wants).

    But if I make a category5.php with nextpostTRUE and test to what category the post belongs, my “new visitor” will have a problem, not getting what he expects.

    So in programming terms I look for:

    When single is accessed from index use and keep using nextpostNOTTRUE
    else When single is accessed from (any)category use and keep using nextpostTRUE.

    Well, at least this is a brain stretcher for me.



    Ditto that – I came here looking for a solution to this same problem.. but the more i think about it, the more i think it’s not possible.

    how is the single post page to know where you came from? think of it like this – if someone posted a link to a single post – when someone else clicked that link, where would “next post” navigate to..? (and, having not come from either your home page OR a specific category – the new user will generally expect next/previous to go by post date, so if it does not, you’re seriously messing with usability issues)..

    next i thought a solution may be to offer two next/previous links on any given single post – next/previous (by post date) AND next/previous by category – HOWEVER – that doesn’t work if you filed the post under more than one category (which all of mine are)..

    so no – i don’t think it’s possible to do EXACTLY what you want to do – however i will be happy if someone smarter than me can prove me wrong 😉



    What about using a drop down menu that somehow passes the selected category through the link to the next page? At least, that would be helpful for me, and seems like it matches up with these problems.

    On my site, there are three distinct areas. One for Monday updates, one for Wednesday updates, and one for Friday updates. On the homepage, “back”, “next”, etc., cycle through them all.

    What I’d like to be able to do is have a drop down that says “currently browsing” or something like that, and have choices be:

    If the user selects one, that will be what the nav buttons control until the user changes it. Is it possible to have them select that, and have the info passed to the next page? It sounds to me like it would be similar to the IF statements above, but it would default to whatever was already selected.

    Does that make conceptual sense to anyone? If so, I would very much appreciate any ideas of how to actually implement that or sample code, as I’m not so strong with actual code syntax.

Viewing 15 replies - 1 through 15 (of 37 total)
  • The topic ‘Previous/Next Post in Category?’ is closed to new replies.