php - Previous and next link in wordpress for the same category -
in single.php file of wordpress there navigation section link next or previous post. code used previous/next post. these links open same category posts (in example below "2" id of category):
<?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 not working?
the fourth parameter categories want exclude, in case you're excluding category 2.
removing fourth parameter should 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 , previous_post_link.
update
get adjacent post links 1 subcategory not simple, can apply method described in this answer, using filter wp_get_object_terms refer category wanted.
so, having id of category want (in case use 2 example), code put in single.php file:
<?php // set category id wanted $globals['just_this_category'] = 2; // add filter 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 after navigation links remove_filter('wp_get_object_terms', 'my_custom_post_navigation'); ?> and filter function functions.php file:
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 can see used global variable $just_this_category pass category id filter function.
obviously every post need set different category id (you can retrieve automatically, how depends on how manage categories).
Comments
Post a Comment