In single.php file of wordpress there is a navigation section to link the next or previous post. This is the code I used for the previous/next post. I would like that these links open the same category posts (in my example below "2" is the ID of the category):
在wordpress的single.php文件中,有一个链接下一个或上一个帖子的导航部分。这是我上一篇/下一篇文章中使用的代码。我希望这些链接打开相同的类别帖子(在我的示例中,“2”是类别的ID):
<?php previous_post_link('%link', '<i class="icon-chevron-left pull-left"></i>', TRUE, '2'); ?>
<?php next_post_link('%link', '<i class="icon-chevron-right pull-right"></i>', TRUE, '2'); ?>
Why it is not working?
为什么不工作?
1 个解决方案
#1
The fourth parameter is for the categories you want to exclude, so in this case you're excluding the category 2
.
第四个参数适用于您要排除的类别,因此在这种情况下,您将排除类别2。
Removing the fourth parameter should do the trick:
删除第四个参数应该可以解决问题:
<?php previous_post_link('%link', '<i class="icon-chevron-left pull-left"></i>', TRUE); ?>
<?php next_post_link('%link', '<i class="icon-chevron-right pull-right"></i>', TRUE); ?>
Reference: next_post_link and previous_post_link.
参考:next_post_link和previous_post_link。
Update
Get adjacent post links just for one subcategory is not so simple, but you can apply the method described in this answer, using the filter wp_get_object_terms
to refer just to the category wanted.
仅为一个子类别获取相邻的帖子链接并不是那么简单,但您可以应用此答案中描述的方法,使用过滤器wp_get_object_terms来引用所需的类别。
So, having the ID of the category you want (in this case I use 2
like your example), this would be the code to put in your single.php
file:
所以,拥有你想要的类别的ID(在这种情况下,我使用2就像你的例子),这将是放在你的single.php文件中的代码:
<?php
// set the category ID wanted
$GLOBALS['just_this_category'] = 2;
// add filter for navigation links
add_filter('wp_get_object_terms', 'my_custom_post_navigation'); ?>
?>
<!-- navigation links -->
<?php previous_post_link('%link', '<i class="icon-chevron-left pull-left"></i>', TRUE); ?>
<?php next_post_link('%link', '<i class="icon-chevron-right pull-right"></i>', TRUE); ?>
<?php // remove filter just after navigation links
remove_filter('wp_get_object_terms', 'my_custom_post_navigation');
?>
And this the filter function for your functions.php
file:
这是你的functions.php文件的过滤函数:
function my_custom_post_navigation($terms){
global $just_this_category;
if( array_search($just_this_category, (array)$terms) !== FALSE )
return array($just_this_category);
return array();
}
As you can see I used the global variable $just_this_category
to pass the category ID to the filter function.
如您所见,我使用全局变量$ just_this_category将类别ID传递给过滤器函数。
Obviously for every post you need to set a different category ID (you can retrieve it automatically, but how to depends on how you manage your categories).
显然,对于每个帖子,您需要设置不同的类别ID(您可以自动检索它,但如何取决于您管理类别的方式)。
#1
The fourth parameter is for the categories you want to exclude, so in this case you're excluding the category 2
.
第四个参数适用于您要排除的类别,因此在这种情况下,您将排除类别2。
Removing the fourth parameter should do the trick:
删除第四个参数应该可以解决问题:
<?php previous_post_link('%link', '<i class="icon-chevron-left pull-left"></i>', TRUE); ?>
<?php next_post_link('%link', '<i class="icon-chevron-right pull-right"></i>', TRUE); ?>
Reference: next_post_link and previous_post_link.
参考:next_post_link和previous_post_link。
Update
Get adjacent post links just for one subcategory is not so simple, but you can apply the method described in this answer, using the filter wp_get_object_terms
to refer just to the category wanted.
仅为一个子类别获取相邻的帖子链接并不是那么简单,但您可以应用此答案中描述的方法,使用过滤器wp_get_object_terms来引用所需的类别。
So, having the ID of the category you want (in this case I use 2
like your example), this would be the code to put in your single.php
file:
所以,拥有你想要的类别的ID(在这种情况下,我使用2就像你的例子),这将是放在你的single.php文件中的代码:
<?php
// set the category ID wanted
$GLOBALS['just_this_category'] = 2;
// add filter for navigation links
add_filter('wp_get_object_terms', 'my_custom_post_navigation'); ?>
?>
<!-- navigation links -->
<?php previous_post_link('%link', '<i class="icon-chevron-left pull-left"></i>', TRUE); ?>
<?php next_post_link('%link', '<i class="icon-chevron-right pull-right"></i>', TRUE); ?>
<?php // remove filter just after navigation links
remove_filter('wp_get_object_terms', 'my_custom_post_navigation');
?>
And this the filter function for your functions.php
file:
这是你的functions.php文件的过滤函数:
function my_custom_post_navigation($terms){
global $just_this_category;
if( array_search($just_this_category, (array)$terms) !== FALSE )
return array($just_this_category);
return array();
}
As you can see I used the global variable $just_this_category
to pass the category ID to the filter function.
如您所见,我使用全局变量$ just_this_category将类别ID传递给过滤器函数。
Obviously for every post you need to set a different category ID (you can retrieve it automatically, but how to depends on how you manage your categories).
显然,对于每个帖子,您需要设置不同的类别ID(您可以自动检索它,但如何取决于您管理类别的方式)。