Archived topic
Two or more categories for one post menu effect
6 replies · Started by Salvador on July 17, 2018
Hi,
I have the following problem ...
I want my posts to have different categories, and that the categories are in the menu. The problem is that this implies that when viewing the post, all the categories change their effect in the menu.
How could I solve it?

Only one category

many categories
Hi there,
You can try using Custom Link for menu item and add the link to the category page as the URL:
https://docs.generatepress.com/article/using-the-wordpress-menu-builder/#custom-link
Hi Leo,
Thanks for your quick reply.
Yes, what you comment works ... half.
They no longer 'highlight' or change the color of the menu categories ... but my intention was to know if there was any way to select a category 'main' for the post (if you have several) to continue making the effect .
Is there any other way?
Thanks.
I think you will have to try something like this:
https://joshuawinn.com/using-yoasts-primary-category-in-wordpress-theme/
Untested so I'm not sure if this will work for the menu items also. Worth a shot though.
I just tried, both with Yoast SEO and another SEO plugin, but it has not worked.
I guess it's something impossible :(
I don't think so but I'll check with Tom :)
You could maybe try something like this:
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );
function my_special_nav_class( $classes, $item ) {
$posts_to_watch = array(
'A Single Post Title',
'Another Post Title',
'One More Post Title',
);
foreach ( $posts_to_watch as $post ) {
if ( is_single( $post ) && $post === $item->title ) {
$classes[] = 'current-menu-item';
}
}
return $classes;
}
So you'd have to:
1. Keep using custom links where nothing gets highlighted.
2. Update the array of $posts_to_watch with the titles of your posts you want to highlight.
Just a theory - I haven't tested it.