- This topic has 9 replies, 5 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
January 14, 2023 at 8:02 am #2495433
Bill
I have core content in a range of topics that I want included in the post navigation. No problem there.
I have one category that I want excluded from the post navigation.
I couldn’t see a filter available to do this, so I looked at the underlying theme code. It seemed like I could create a filter hook and then add a code snippet. That didn’t seem to work.
But it looked like this:
add_filter( 'generate_post_navigation_exclusion_terms', function() { return '13'; } );I’ve also tried this:
add_filter( 'generate_post_navigation_args', function( $args ) { if ( is_single() ) { $args['excluded_terms'] = '13'; } return $args; } );Each time, I still see a link to the post in that category.
Is there a simpler and reliable way to achieve the results I’m looking for?
January 14, 2023 at 11:07 am #2495592Bill
As I look more at this, I am using the Ultimate Category Excluder where I exclude the category from the Front Page.
The default navigation works properly.
The post navigation element doesn’t work properly and I wonder if it’s possible there is a bug. In theory, it’s the same query that’s being modified by the plugin.
January 14, 2023 at 11:48 am #2495619Ying
StaffCustomer SupportHi Bill,
Try this solution:
https://generatepress.com/forums/topic/post-navigation-how-exclude-a-category-from-next-and-previous-post-links/page/2/#post-2247795Replace the
29with the category ID you want to exclude.January 14, 2023 at 11:58 am #2495626Bill
Thanks for the link.
I write code for a living, so I’m not afraid of coding. But that is too much code to have to add to accomplish this task. I should be able to add a filter (if one doesn’t exist) and then add a function for that filter.
I should not have to replace entire functions as that solution prescribes. If that function takes on more responsibilities over time or changes its interface, then I will potentially have problems.
I think generatepress is missing a useful filter and/or has a bug in the post navigation implementation.
January 14, 2023 at 12:37 pm #2495697Ying
StaffCustomer SupportI think generatepress is missing a useful filter and/or has a bug in the post navigation implementation.
There was a WP bug so the below code could not work, can you give it another try?
add_filter( 'get_next_post_excluded_terms', 'tu_exclude_terms' ); add_filter( 'get_previous_post_excluded_terms', 'tu_exclude_terms' ); function tu_exclude_terms() { return array( '29' ); }January 22, 2023 at 10:36 pm #2505389Daniel
It seems the filter assumes the list of id’s is an array, so you do not have to declare it. This works for me:
add_filter( ‘get_next_post_excluded_terms’, ‘tu_exclude_terms’ );
add_filter( ‘get_previous_post_excluded_terms’, ‘tu_exclude_terms’ );
function tu_exclude_terms() {
return ‘304,30169’;
}March 9, 2023 at 2:48 am #2561201Eva
Hi, I would like to use this filter too. Can you please explain how I can add it? Should I use the wp_head in the Hooks Element?
Thank you!
EvaMarch 9, 2023 at 3:38 am #2561246David
StaffCustomer SupportHi Eva,
that code is PHP, and it cannot be added to a GP Element.
This article explains how to add the code:March 9, 2023 at 3:56 am #2561287Eva
Thank you, David. I just wanted to be sure and will use the Code Snippets plugin then.
March 9, 2023 at 5:22 am #2561393David
StaffCustomer SupportGlad to be of help!
-
AuthorPosts
- You must be logged in to reply to this topic.