1

I am using a category to segregate all posts of that one category to feel like a complete website sub-section. So the main posts feed is using a plugin to hide posts of that category, and I am using a menu item in the header to link to a page showing posts of only that category.

However, once you click on a post, the next and previous will browse through all posts regardless of categories and I would want it to remain within the context of that category.

Likewise, in the full regular feed, once a post is open I would like the navigation to exclude that category.

2
  • Are you using query_posts or a custom WP_Query to display your posts?
    – Tom J Nowell
    May 2, 2017 at 15:02
  • I am not intentionally using anything, i.e. I'm using it out of the box. I am using some plugins but none that affect the display of posts. May 2, 2017 at 15:05

1 Answer 1

0

Choose from any of the following functions:

  1. get_next_post_link();
  2. next_post_link();
  3. get_previous_post_link();
  4. previous_post_link();

Parameters are as follows (in respective order):

  1. @param string $format Optional. Link anchor format. Default '« %link'.
  2. @param string $link Optional. Link permalink format. Default '%title'.
  3. @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
  4. @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  5. @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.

@return string The link URL of the next post in relation to the current post.

Optionally, you could do more with your pagination if you opted for more labor. You could use get_next_post(); or get_previous_post(); These two functions would allow you to retrieve the entire next/previous post object. You could then retrieve post meta, featured image, post format, post excerpt, etc.

Parameters are as follows (in respective order):

  1. @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false.
  2. @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
  3. @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.

@return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.

All of this (and more) can be found in ./wp-includes/link-template.php

2
  • Thank you. I am only new to Wordpress and rely mostly on plugins. What I'm unsure on is where to use the four functions you first mention and how do I restrict what I want to only a certain category? May 2, 2017 at 14:57
  • I would search your currently activated WordPress theme for the usage of those functions. Find their usage of those functions and replace with your own usage of these functions. Depending on how the theme was crafted those functions could be called in a number of different ways. I would check: single.php, singular.php, page.php, etc. Now I would also recommend using a Child Theme to make your changes, so that your changes aren't lost when the theme gets updated. May 2, 2017 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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