Single post: Show posts "only" from the category with the next/previous links? (5 posts)

  1. Agent D
    Posted 6 years ago #


    I bumped into this problem and I just can't figure it out...

    I've customized the single.php file, so that now when it's in category 3 it automatically loads "single-3.php" which works fine. BUT the problem is that the links above the post which show next and previous posts doesn't distinguish the category, so that if you click through them you end up seeing the posts from other category and that's is a problem because the single post page has a graphical header for category 3.

    Is there anyway that I can limit the next/previous links to show only from category 3?

    If you have any idea or have experience with this please let me know. Many thanks!


  2. esmi
    Forum Moderator
    Posted 6 years ago #

    Have you tried something like:

    <?php next_post_link('%link', 'Next post in category', TRUE); ?>


  3. Agent D
    Posted 6 years ago #

    Hmm... I just tried it but still shows posts from other categories... I also would like to show the title of the post instead of "next post in category". Any other suggestions? Thank you.


  4. Agent D
    Posted 6 years ago #

    Ok, I figured out how to post display the title but still haven't been able to figure out the category issue. The thing is that Category 3 is a child category(or sub category) of Category 1. DO you think that is the issue?


  5. vtxyzzy
    Posted 6 years ago #

    I had the same problem. I renamed 'single.php' to 'single-default.php', then made a copy of it to 'single-in-category.php'.

    In my theme, I only needed to change two lines in 'single-in-category.php'.


    <div class="alignleft"><?php previous_post_link('&laquo; %link &laquo;') ?></div>
    <div class="alignright"><?php next_post_link('&raquo; %link &raquo;') ?></div>


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

    Then I wrote a new 'single.php' to call 'single-in-category.php' if it was one of the categories I wanted to restrict, otherwise, call 'single-default.php'.

    I am a relative newbie to WP, so there might be an easier way, but this worked for me.

Topic Closed

This topic has been closed to new replies.

About this Topic