I have posts that are in multiple categories. I am showing a next/previous post link on posts that are in a specific category, regardless of how many other categories this post has. The links are now to posts that are in any of the categories that the post is in. I want to only link to the next/previous post in one of the categories the post is in.
I found a function online that I have tried to change to suit my needs. This is in my functions.php file:
//Next and previous post logic
function get_excluded_categories() {
// Get categories of current post
$current_cats = wp_get_post_terms( get_the_ID(), 'category' );
// Declare array to store excluded categories
$excluded_cats = [];
// Loop through current categories
foreach( $current_cats as $cat ) {
// Check if current category is included
$this_cat = $cat->slug;
if($this_cat !== '[SLUG]') {
// If it is not, add it's term ID to the excluded array
array_push($excluded_cats, $cat->term_id);
}
}
return $excluded_cats;
}
This is how I display the links:
<?php if (in_category('[SLUG]')) : ?>
<div class = "next-post"><?php next_post_link('%link »', '%title', true, get_excluded_categories()); ?></div>
<div class = "prev-post"><?php previous_post_link('« %link', '%title', true, get_excluded_categories()); ?></div>
<?php endif; ?>
This is not working quite as expected. Only one link shows up, either the next or the previous post link. If I have only two posts in the category, this is fine and expected behavior. But if I have three posts, I want to have only the next link on the first, the next and previous links on the middle post and only the previous link on the last post.
Right now it seems to just skip over any existing posts in between the first and last. It is actually not the posts "in between" that are skipped over, but the second post does not have a link to the third post. If I go manually into the third post, it has a previous link to the first post and not the second as expected.
I know I have probably got my logic wrong, but I don't understand how, where or why.
Any help is greatly appreciated!
I have posts that are in multiple categories. I am showing a next/previous post link on posts that are in a specific category, regardless of how many other categories this post has. The links are now to posts that are in any of the categories that the post is in. I want to only link to the next/previous post in one of the categories the post is in.
I found a function online that I have tried to change to suit my needs. This is in my functions.php file:
//Next and previous post logic
function get_excluded_categories() {
// Get categories of current post
$current_cats = wp_get_post_terms( get_the_ID(), 'category' );
// Declare array to store excluded categories
$excluded_cats = [];
// Loop through current categories
foreach( $current_cats as $cat ) {
// Check if current category is included
$this_cat = $cat->slug;
if($this_cat !== '[SLUG]') {
// If it is not, add it's term ID to the excluded array
array_push($excluded_cats, $cat->term_id);
}
}
return $excluded_cats;
}
This is how I display the links:
<?php if (in_category('[SLUG]')) : ?>
<div class = "next-post"><?php next_post_link('%link »', '%title', true, get_excluded_categories()); ?></div>
<div class = "prev-post"><?php previous_post_link('« %link', '%title', true, get_excluded_categories()); ?></div>
<?php endif; ?>
This is not working quite as expected. Only one link shows up, either the next or the previous post link. If I have only two posts in the category, this is fine and expected behavior. But if I have three posts, I want to have only the next link on the first, the next and previous links on the middle post and only the previous link on the last post.
Right now it seems to just skip over any existing posts in between the first and last. It is actually not the posts "in between" that are skipped over, but the second post does not have a link to the third post. If I go manually into the third post, it has a previous link to the first post and not the second as expected.
I know I have probably got my logic wrong, but I don't understand how, where or why.
Any help is greatly appreciated!
Share Improve this question edited May 8, 2019 at 10:55 TASan asked May 8, 2019 at 10:39 TASanTASan 421 silver badge9 bronze badges 1- Is this because WordPress is going: "Does this post have category X?" Rather than: "Let's not look through this category since that is excluded" Because the former is really disregarding the fact that posts can be in several categories at once. – TASan Commented May 8, 2019 at 13:13
1 Answer
Reset to default 0I solved it by abandoning the code and registering a new taxonomy. I then used this code in single.php:
<?php if (has_term('[TERM-NAME]', '[TAXONOMY-NAME]')) : ?>
<div class = "next-post"><?php next_post_link('%link »', '%title', true, '', '[TAXONOMY-NAME]') ?></div>
<div class = "prev-post"><?php previous_post_link('« %link', '%title', true, '', '[TAXONOMY-NAME') ?></div>
<?php endif; ?>
Doing it like this allows me to have multiple categories since I only assign one term in the custom taxonomy per post.