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 |1 Answer
Reset to default 0Choose from any of the following functions:
get_next_post_link();
next_post_link();
get_previous_post_link();
previous_post_link();
Parameters are as follows (in respective order):
@param
string
$format
Optional. Link anchor format. Default '« %link'.@param
string
$link
Optional. Link permalink format. Default '%title'.@param
bool
$in_same_term
Optional. Whether link should be in a same taxonomy term. Defaultfalse
.@param
array|string
$excluded_terms
Optional.Array
or comma-separated list of excluded term IDs. Defaultempty
.@param
string
$taxonomy
Optional. Taxonomy, if$in_same_term
istrue
. 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):
@param
bool
$in_same_term
Optional. Whether post should be in a same taxonomy term. Defaultfalse
.@param
array|string
$excluded_terms
Optional.Array
or comma-separated list of excluded term IDs. Defaultempty
.@param
string
$taxonomy
Optional. Taxonomy, if$in_same_term
istrue
. Default 'category'.
@return
null|string|WP_Post
Post object
if successful.Null
ifglobal $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
版权声明:本文标题:categories - How to Restrict Previous & Next Post Link to Posts of Same Category? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282375a1926625.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
query_posts
or a customWP_Query
to display your posts? – Tom J Nowell ♦ Commented May 2, 2017 at 15:02