• I am using a single template for a specific category: single-5.php
    In this file I have navigation links, for example:

    next_post_link(‘%link’, ‘ <span class=”meta-nav”>«</span>’, TRUE)

    I expect to always navigate to the next post in category 5, but I’m getting posts from other categories and I can’t figure out why this isn’t working after reading the documentation on this function.

    Thanks for any help,
    C

Viewing 3 replies - 1 through 3 (of 3 total)
  • Are your posts in just one category, or multiple categories? I’ve seen this behave weirdly when a post is in more than 1 category.

    You can also try using something like the Query Monitor plugin to see what SQL query is generated for this link.

    Also check to see if you have other plugins that deal with categories, such as Yoast SEO. They might be filtering the next_post_link function (or rather, the parent function, get_adjacent_post_link). See the code for this here: https://core.trac.wordpress.org/browser/tags/4.5.3/src/wp-includes/link-template.php#L1964

    Hello

    Try below code, Hope this works for you!

    next_post_link( '%link','<span class="meta-nav">«</span>', true, ' ', 'category' );

    • This reply was modified 7 years, 3 months ago by Mithun Raval.
    • This reply was modified 7 years, 3 months ago by Mithun Raval.
    Thread Starter coriander

    (@coriander)

    Hi, thanks for the replies.
    I tried the above code but I have the same issue.
    Each post in this site is in a single category, so I know that isn’t the problem.
    I’ll try the Query Monitor next to see what exactly is happening.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘next_post_link not limiting by category’ is closed to new replies.