- This topic has 30 replies, 4 voices, and was last updated 4 years, 10 months ago by Leo.
-
AuthorPosts
-
May 15, 2019 at 12:19 am #899745Elia
Hi there,
Could you please help me exclude the category from the “Previous Next articles” section?
May 15, 2019 at 1:09 am #899791EliaBy 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 sectionPlease let me know if I’m nor clear
May 15, 2019 at 4:12 am #899946DavidStaffCustomer SupportHi 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
May 15, 2019 at 4:34 am #899963EliaThanks, 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 linksWe 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
May 15, 2019 at 4:49 am #899978DavidStaffCustomer SupportIf i understand correctly, then the Function that Tom provides here is what you need for point 1:
May 15, 2019 at 4:51 am #899982EliaThanks 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
May 15, 2019 at 5:12 am #900005DavidStaffCustomer SupportI 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?
May 15, 2019 at 5:14 am #900008EliaYes, David. Thanks a lot!
May 15, 2019 at 9:07 am #900389TomLead DeveloperLead DeveloperHi 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; }
May 16, 2019 at 3:45 am #901245EliaHi 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?
May 16, 2019 at 5:41 pm #902049TomLead DeveloperLead DeveloperHi 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 ๐
May 28, 2019 at 1:15 am #912909EliaHi 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
May 28, 2019 at 8:22 am #913451LeoStaffCustomer SupportI think you will need to change
Category C
and10
May 28, 2019 at 8:30 am #913459EliaThanks Leo, do I have to change the “Category C” to the exact name of the category? I tried that it doesn’t work
May 28, 2019 at 12:40 pm #913670TomLead DeveloperLead DeveloperCan you share the function you’re using?
Also, can you link me to the page where it should be working?
Thanks!
-
AuthorPosts
- You must be logged in to reply to this topic.