Archived topic
Exclude a category from "Previous Next articles"
30 replies · Started by Elia on May 15, 2019
Hi there,
Could you please help me exclude the category from the "Previous Next articles" section?
By exclude I meant 2 things.
1) When you go to the blog and go to the previous/next articles you shouldn't see links to 1 of the categories
2) However, when we open this category directly on the website you should be able to other link within this categories using the previous/next articles section
Please let me know if I'm nor clear
Hi there,
you can remove the category link in Customizer > Layout > Blog --> Single Tab > Display Post Cateogries
To limit the scope of the post links to the current category you can use the filter that Tom provides here:
https://generatepress.com/forums/topic/behavior-of-next-previous-post-links/#post-884910
Thanks, David!
However, this is not what we need.
Let's say we have 4 categories:
Category A links
Category B links
Category C links
Category D links
We would like to isolate all links within one of the categories.
To do so we need 2 things:
1) Exclude Category C links from the previous/next section when you visit posts from other categories. As a result when let's say you visit a post from the Category A, you won't see a link to Any post leading to Category C in the previous/next section
2) This step is OPTIONAL (If it's hard to do then just the first thing is fine).
When you visit a category Category C and go to the previous/next section you should only see the links within Category C.
Please let me know if I'm not clear
If i understand correctly, then the Function that Tom provides here is what you need for point 1:
Thanks David,
The Tom's code is to isolate each category from each other. In our case we need to isolate just 1 category from others
I think this may require some custom development, so before we look at to see if we can help lets make sure i fully understand :)
Posts within Category C
Post Navigation to be limited to posts within Category C only.
Posts not within Category C
Post Navigation to work as per default but to exclude Posts from Category C.
Is that correct?
Yes, David. Thanks a lot!
Hi there,
I'm not 100% sure this will work, but it's worth a shot:
add_filter( 'generate_category_post_navigation', function( $in_same_term ) {
if ( has_category( 'Category C' ) ) {
$in_same_term = true;
}
return $in_same_term;
} );
add_filter( 'get_next_post_excluded_terms', 'tu_filter_excluded_terms' );
add_filter( 'get_previous_post_excluded_terms', 'tu_filter_excluded_terms' );
function tu_filter_excluded_terms( $excluded_terms ) {
if ( ! has_category( 'Category C' ) ) {
$excluded_terms[] = 10; // The ID of Category C.
}
return $excluded_terms;
}
Hi Tom,
thanks a lot!
For some reason the code doesn't work. Could it be because my posts belongs not to category itself but to subcategories?
Hi there,
Did neither function work? The second one is somewhat experimental, but the first one should work if the has_category() condition is correct.
Let me know :)
Hi Tom,
Do I need to just change the category ID in your code, right?
If yes, then the code doesn't work for some reason
I think you will need to change Category C and 10
Thanks Leo, do I have to change the "Category C" to the exact name of the category? I tried that it doesn't work
Can you share the function you're using?
Also, can you link me to the page where it should be working?
Thanks!