admin管理员组

文章数量:1122832

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.

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.

Share Improve this question edited Mar 23, 2018 at 15:01 Michael Ecklund 6,81213 gold badges68 silver badges110 bronze badges asked May 2, 2017 at 13:03 Ghandi ManningGhandi Manning 1115 bronze badges 2
  • Are you using query_posts or a custom WP_Query to display your posts? – Tom J Nowell Commented 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. – Ghandi Manning Commented May 2, 2017 at 15:05
Add a comment  | 

1 Answer 1

Reset to default 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

本文标签: categoriesHow to Restrict Previous amp Next Post Link to Posts of Same Category