As the title: is there a way to navigate between posts of the same subcategory using the previous_post_link and next_post_link functions? But maintaining however both the category and the sub-category of the post.


If you’ll take a look at next_post_link Codex page, you’ll see it’s usage:

    $in_same_term   = false,
    $excluded_terms = '',
    $taxonomy       = 'category'

As you can see, third param is in_same_term and it’s default value is false. If you set it to true, then the function will return next post from same category. (If you want the term from different taxonomy, you can change fifth param).

So answering your question... Yes, there is a way to achieve this. It can look like so:

<?php next_post_link( '%link &raquo;', '%title', true ); ?>
  • thanks for replay i'm using this parameter ( '&larr;', 'Previous post link', 'twentytwelve', true), but it does not work I get the previous link in the parent category category/subcategory/post-n3 the p-link instead of going to: category/subcategory/post-n2 go to: category/post-n1 – claudio Jan 23 '19 at 15:53
  • But you do you pass 'twentytwelve' as third param? – Krzysiek Dróżdż Jan 23 '19 at 15:54
  • it has remained from the used snippets but this is not the problem, I have just tested it as a third parameter ( '&larr;', 'Previous post link', true) – claudio Jan 23 '19 at 16:03
  • You can’t set random values for params and expect them to work... third param should be true... not some random string. And it will work only if the given post has only one term in given taxonomy (otherwise it will take first of terms, AFAIR) – Krzysiek Dróżdż Jan 23 '19 at 16:05
  • the thing has never given problems for the posts in the same category in tot developed themes, while it does not work for the subcategorie as it does not work with true as the third parameter.. – claudio Jan 23 '19 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.