-
philipperoussel
Hi,
How can I remove a specific post category from the following rule:
add_action( ‘after_setup_theme’, ‘tu_category_specific_post_navigation’ );
function tu_category_specific_post_navigation() {
add_filter( ‘generate_category_post_navigation’, ‘__return_true’ );
}Thanks for your help,
Philippe
-
Hi there,
Try this:
add_action('wp', 'custom_generate_category_post_navigation'); function custom_generate_category_post_navigation() { if ( !in_category('cat-x')) { add_filter('generate_category_post_navigation', '__return_true' ); } }
Replace the
cat-x
with your category slug: https://codex.wordpress.org/Conditional_Tags#A_Category_PageLet me know if this helps 🙂
-
philipperoussel
Hi Leo,
I replaced cat-x with ‘articles’ (the concerned post category) but it does not work.
-
Hi there,
Try this snippet:
add_action('after_setup_theme', function() { add_filter('generate_category_post_navigation', function($display) { // List of category slugs to exclude $excluded_slugs = ['articles']; // 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; }); });
-
philipperoussel
Hi Alvin,
I did the following. It still does not work. No big deal if that cannot be achieved.
add_action( ‘after_setup_theme’, ‘tu_category_specific_post_navigation’ );
function tu_category_specific_post_navigation() {
add_filter( ‘generate_category_post_navigation’, ‘__return_true’ );
$excluded_slugs = [‘articles’];
} -
Hi there,
can you share a link to a single post where the navigation needs to be removed ?
-
philipperoussel
Hi David,
This one, for instance: https://onehomeplanet.com/the-end-of-zionism/
-
Alvind
Have you tried adding the entire snippet I provided above?
-
philipperoussel
Hi Alvind,
Sorry for the long delay for my answer. I did try the entire snippet you provided, unfortunately it does not work. I also added $excluded_slugs = [‘articles’]; to the original code I have (this one works for what it has to do: showing nav. links), but to no avail.
Thanks again for your help.
-
Hi there,
the method Leo provided should work:
add_action('wp', 'custom_generate_category_post_navigation'); function custom_generate_category_post_navigation() { if ( !in_category('articles')) { add_filter('generate_category_post_navigation', '__return_true' ); } }
-
philipperoussel
Hi,
It does not work either.
-
How did you add the PHP code?
And with David’s code, you need to disable the
Display post navigation
option at customizer > layout > blog > content > single. -
philipperoussel
Hi,
I added the code in a PHP snippet.
Disabling the post navigation goes against what I want, which is to have it disabled for specific post categories, not all of them.
-
Alvind
In this case, you need to disable the post navigation because the snippet will enable post pagination where the current post is not within the category you chose to exclude in the snippet.
-
philipperoussel
Hi,
I disabled the post navigation. The following code does not activate post navigation for post categories other than the one mentioned:
add_action(‘wp’, ‘custom_generate_category_post_navigation’);
function custom_generate_category_post_navigation() {
if ( !in_category(‘articles’)) {
add_filter(‘generate_category_post_navigation’, ‘__return_true’ );
}
} -
Hmmm… something seems to be conflicting.
How about some CSS to hide it instead ?.single-post .category-articles #nav-below { display: none; }
- You must be logged in to reply to this topic.