• DoubleG

    (@doubleg)


    I am using the previous_post_link() & next_post_link() functions to navigate to previous and next posts but only want it to show/navigate to post within the same category.

    In my example the category I want to use is the “News” category which has a “category&tag_ID=16”.

    I’ve read the wordpress Codex about this function here but am still not sure how to edit the parameters within the function so it only shows previous and next posts from a specific category.

    NB. The posts on this website often use multiple categories but as explained earlier, the “news” category (16) is the one I’m looking to use.

    Currently the code I am using is as follows:

    <div class="posts_navigation_single">
    <div class="posts_prev_single">
    <?php previous_post_link(); ?>
    </div>
    <div class="posts_next_single">   		
    <?php next_post_link(); ?>
    </div>
    </div>

    I’d very much appreciate any help in the above subject.

    Many thanks

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • undiscoveredhouse

    (@undiscoveredhouse)

    Looking at codex, you’ll need to set the $in_same_term parameter to true. Try replace the previous_post_link(); in your code with this snippet below:

    previous_post_link( '%link', '%title', true );

    The same syntax format applies for next_post_link(); as well

    Thread Starter DoubleG

    (@doubleg)

    Fantastic, worked a treat!

    I was unsure how to deal with the parameters that appear before the $in_same_term parameter. So i guess you just insert them as a “blank” variable inside the ‘ ‘.

    Very useful to know and thanks again.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘previous_post_link() – Only in one category’ is closed to new replies.