-
Hi! I am using both pages and posts for creating content in my webpage, so I use the post navigation element in both.
I have created a category (slug is “interno” and category ID is 1370) which contains some pages I don’t want to show in post navigation element (such as cookies policy, legal information…).
I have tried adding some code I found in this forum to hide that category, but it`s not working. This is the code I added to functions.php:
/** * Remove Post Navigation in categories */ add_action('after_setup_theme', function() { add_filter('generate_category_post_navigation', function($display) { // List of category slugs to exclude $excluded_slugs = ['interno']; // Get the current post's category slugs $post_categories = get_the_category(); $post_slugs = wp_list_pluck($post_categories, 'slug'); // Check if the current post is in any of the excluded categories $is_excluded = !empty(array_intersect($excluded_slugs, $post_slugs)); // If the post is in an excluded category, return false to disable navigation // Otherwise, return the original value return $is_excluded ? false : $display; }); });It’s not working so far so I need to Know what I am doing wrong. Thank you in advance!
-
Hi there,
that code will only work with the themes default post navigation.
And I am not 100% sure on how this will work with an element – but you could try this:add_filter('get_previous_post_where', 'exclude_category_from_adjacent_posts'); add_filter('get_next_post_where', 'exclude_category_from_adjacent_posts'); function exclude_category_from_adjacent_posts($where) { $where .= " AND p.ID NOT IN (SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id = YOUR_CATEGORY_ID)"; return $where; }Replace the
YOUR_CATEGORY_IDwith the ID . -
Hi David,
Unfortunately, the code you provided is not working either.
Yes, I want to exclude a category of pages from showing in the default post navigation ( this: Block Element – Post Navigation).
Can you please provide me with the correct code?
-
A block element – post navigation is not the default post navigation of the theme.
However, give this code a try:
add_filter('get_previous_post_where', 'exclude_category_from_adjacent_posts'); add_filter('get_next_post_where', 'exclude_category_from_adjacent_posts'); function exclude_category_from_adjacent_posts($where) { // Get the term ID of the "interno" category $exclude_category_id = get_cat_ID('interno'); // Replace 'interno' with your category slug if needed // Add condition to exclude posts in the "interno" category if ($exclude_category_id) { global $wpdb; $where .= " AND p.ID NOT IN (SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id = {$exclude_category_id})"; } return $where; }
- You must be logged in to reply to this topic.