WordPress.org

Ready to get started?Download WordPress

Forums

Previous/Next Post in Category? (38 posts)

  1. katana_one
    Member
    Posted 5 years ago #

    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?

  2. asechrest
    Member
    Posted 5 years ago #

    I think you'll find the answer here.

    in_same_cat argument should be set to TRUE.

  3. katana_one
    Member
    Posted 5 years ago #

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

  4. katana_one
    Member
    Posted 5 years ago #

    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?

  5. asechrest
    Member
    Posted 5 years ago #

    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.

  6. katana_one
    Member
    Posted 5 years ago #

    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. :)

  7. asechrest
    Member
    Posted 5 years ago #

    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.

  8. revdpeter3rd
    Member
    Posted 5 years ago #

    Hi,

    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,
    Rev

  9. asechrest
    Member
    Posted 5 years ago #

    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.

  10. henkholland
    Member
    Posted 5 years ago #

    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.

    Anyone?

  11. henkholland
    Member
    Posted 5 years ago #

    My site is live now
    http://www.enduro.nl
    but I still have not found a solution for the above mentioned problem?
    Anyone?

  12. iconico
    Member
    Posted 5 years ago #

    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???

  13. smallina
    Member
    Posted 5 years ago #

    henkholland and iconico:

    Take a look at this:
    http://codex.wordpress.org/Theme_Development#Query-based_Templates

    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!

  14. henkholland
    Member
    Posted 5 years ago #

    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:
    http://www.enduro.nl/wptest/

    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.

  15. invisiblepilot
    Member
    Posted 5 years ago #

    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 ;)

  16. th3dug5
    Member
    Posted 4 years ago #

    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:
    -All
    -Mondays
    -Wednesdays
    -Fridays

    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.

  17. paperflame
    Member
    Posted 4 years ago #

    Ok Guys and Gals.. I searched and searched for a solution... BAM... found a solution.

    I am pretty sure this is what is trying to be accomplished. I stumbled on this thread about previous and next post in category.

    Check this plug-in out! It takes it a step further than just categories - it handles - tag, date, author, search etc. It uses a referrer.. Awesome....
    You can see it in action on my site http://www.paperflame.com/content

    http://wordpress.org/extend/plugins/smarter-navigation/

    Hope this helps if you didn't know about it!

  18. henkholland
    Member
    Posted 4 years ago #

    Paperflame AND SCRIBU: you made my day. This plugin from Scribu solves my problem.
    To recap:
    When single.php is accessed from index.php: use and keep using "nextpostNOTTRUE"
    else When single.php is accessed from (any)category.php use and keep using nextpostTRUE.
    And that's what it does. Super.

  19. atropos90
    Member
    Posted 4 years ago #

    Pleeeeease take a lokk my site any body - what are happining? I really dont understandig! Thanks a lot if you know the problem.

  20. henkholland
    Member
    Posted 4 years ago #

    and where is your site? URL please.

  21. henkholland
    Member
    Posted 4 years ago #

    http://wordpress.org/extend/plugins/smarter-navigation/

    Still works; went from 2.7.1 to 2.8.4 (on my testsite)

  22. Oscar
    Member
    Posted 4 years ago #

    What happens when the post has 2 or more categories assigned. Is there a way to display next and previous specifically for each category.

    Example:
    Post A is found in Cat 1 and Cat 2

    so in the sidebar would be (below):

    Cat 1
    previous post - next post

    Cat 2
    previous post - next post

    Any ideas? Thanks.

  23. amberlaine
    Member
    Posted 4 years ago #

    I've installed the Smarter NAvigation plugin and it is working GREAT if the referrer page is a Date archive.

    However, if the referrer page is the Category archive (or search) it doesn't work at all, and I have no idea why.

    This is my site (still in development):
    http://gracey.technicalpoet.com/

    Any help would be really appreciated. Thanks!

  24. scribu
    Member
    Posted 4 years ago #

    chasix:

    What happens when the post has 2 or more categories assigned. Is there a way to display next and previous specifically for each category.

    Example:
    Post A is found in Cat 1 and Cat 2

    so in the sidebar would be (below):

    Cat 1
    previous post - next post

    Cat 2
    previous post - next post

    Any ideas? Thanks.

    With the Smarter Navigation plugin that is a non-issue, since it will generate links based on the last category page the user came from.

  25. henkholland
    Member
    Posted 4 years ago #

    @ Amberlaine: your site is not accessible at this moment, so I cannot play around.
    "Search"' is not a Category, so I think it would never work on search results. It does not on my site, allthough the Readme txt says it does.

    I am surprised you see it working on a Date Archive apart from being able to flip through all the posts in that month or week, regardless of the category they are in. But that is standard behaviour without the plugin.

    You did put ?php previous_post_smart( etc etc in your single.php ??

  26. marpessa1
    Member
    Posted 4 years ago #

    I'm trying to use this plugin but I cannot seem to get it to work. Does anyone know what to do??

  27. scribu
    Member
    Posted 4 years ago #

    Did you replace previous_post_link() with previous_post_smart() in your theme?

  28. marpessa1
    Member
    Posted 4 years ago #

    Yep, I did, but no effect :(

  29. scribu
    Member
    Posted 4 years ago #

    How do you mean? What are you getting?

  30. travelhorizon
    Member
    Posted 4 years ago #

    Hello Henkholland & Paperflame,
    (if you are still there)!
    This has been so, so useful, but I think I'm missing a small detail.
    I see that using http://wordpress.org/extend/plugins/smarter-navigation/ works on your two sites http://www.paperflame.com/content & http://www.enduro.nl but it has to be configured somehow with the theme Atahualpa and I'm not sure how. So far it's not working for me. I have installed the plugin and put `<?php echo '<div class="newer">'; previous_post_smart();echo '</div>'; ?>
    <?php echo '<div class="older">'; next_post_smart();echo '</div>'; ?>` into ATA->CENTRE COL->THE LOOP (which is the normal single.php on most themes), but if you look at http://travelhorizonstyle.com.au/cat...e-east/turkey/ and then click "Cappadocia & coastal-splendours" the next/prev is not showing all 4 posts (tours), ie (Taste of Turkey) is not displayed at all.
    I have assigned multiple categories to each post (& as the breadcrumb shows).
    When you say:

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

    I'm not sure where to place the "nextpostTRUE"

    This issue will surely help lots of people who assign multitple categories.
    Hoping you can clarify for me.
    Many thanks,
    Joan

Topic Closed

This topic has been closed to new replies.

About this Topic