- This topic has 32 replies, 2 voices, and was last updated 1 year, 3 months ago by David.
-
AuthorPosts
-
December 16, 2019 at 6:02 pm #1106336TomLead DeveloperLead Developer
So in that case, you would do this:
add_filter( 'nav_menu_css_class', function( $classes, $item, $args ) { if ( 'primary' !== $args->theme_location ) { return $classes; } if ( is_singular( 'product' ) && 'Your Menu Item' == $item->title ) { $classes[] = 'current-menu-item'; } return array_unique( $classes ); }, 10, 3 );
Just change
Your Menu Item
to the title of the menu item you want to highlight.December 16, 2019 at 11:27 pm #1106461HarrisThanks Tom. That works fine!
Can it somehow be extended in order to cover the category archive pages too?Thanks
HarrisDecember 17, 2019 at 9:27 am #1107050TomLead DeveloperLead DeveloperSure, try this:
add_filter( 'nav_menu_css_class', function( $classes, $item, $args ) { if ( 'primary' !== $args->theme_location ) { return $classes; } if ( ( is_singular( 'product' ) || is_product_category() ) && 'Your Menu Item' == $item->title ) { $classes[] = 'current-menu-item'; } return array_unique( $classes ); }, 10, 3 );
December 17, 2019 at 10:18 am #1107091HarrisThanks Tom. Perfect!
December 17, 2019 at 6:09 pm #1107358TomLead DeveloperLead DeveloperYou’re welcome 🙂
May 15, 2020 at 1:02 pm #1286300HarrisHello
i am trying to reuse this code to another site for a custom type named “Events” (“Event” in single number) and a menu choice also named “Events”.
I don’t want to aply any category filtering.The code I am using is
function be_menu_item_classes( $classes, $item, $args ) { if ( 'primary' !== $args->theme_location ) { return $classes; } if ( ( is_singular( 'Event' ) ) && 'Events' == $item->title ) { $classes[] = 'current-menu-item'; } return array_unique( $classes ); } add_filter( 'nav_menu_css_class', 'be_menu_item_classes', 10, 3 );
But it doesn’t seem to be working.
What am I doing wrong?I have added the new site url (events single post page)for you to see
May 15, 2020 at 1:05 pm #1286304May 15, 2020 at 4:57 pm #1286529TomLead DeveloperLead DeveloperHi there,
is_singular()
takes the actual post type slug, so yours would be:is_singular( 'koupi_events' )
May 16, 2020 at 12:28 am #1286823HarrisThanks a lot Tom.
Its working.
I thought it was taking the post type name (and not slug)
May 16, 2020 at 10:58 am #1287584TomLead DeveloperLead DeveloperGlad I could help! 🙂
January 16, 2023 at 4:09 am #2497262KirmoHi, I’m planning to use the custom menu items in my main-navigation. And only one level of pages is going to be used in this navigation (those pages are fine), but when entering the child pages or the grandchild pages there would be a need to make that top-item current also. I’m trying to use this modified snippet from here:
add_filter( ‘nav_menu_css_class’, function( $classes, $item, $args ) {
if ( ‘primary’ !== $args->theme_location ) {
return $classes;
}if ( ($post->post_parent == 19) && ‘Päättäjille’ == $item->title ) {
$classes[] = ‘current-menu-item’;
}return array_unique( $classes );
}, 10, 3 );Any tips how to get it working?
See exemples: (main page = OK, page’s ID 19) https://tuleuusi.suomentule.fi/tule-kustannukset/
(child page = not OK) https://tuleuusi.suomentule.fi/tule-kustannukset/sairaanhoitokustannukset/January 16, 2023 at 5:28 am #2497351DavidStaffCustomer SupportHi there,
is there a hierarchical relationship between those pages? If there isn’t then i am not sure its possible as they are two separate menus. Let me know.
January 16, 2023 at 5:48 am #2497376Kirmowasn’t too clear, there is a relation:
https://tuleuusi.suomentule.fi/tule-kustannukset/sairaanhoitokustannukset/is the child page of (ID 19):
https://tuleuusi.suomentule.fi/tule-kustannukset/And only Generatepress main-navigation top level (custom item) is needed to shown as current.
January 16, 2023 at 5:50 am #2497379DavidStaffCustomer SupportAnd is there any relationship between:
Päättäjille
andtule-kustannukset
?January 16, 2023 at 5:57 am #2497387KirmoNo ”Päättäjille” is just a custom menu-item not a page at all. (There wouldn’t be necessary any content there, idea was to have these just as collectors in menu, if it’s possible)
-
AuthorPosts
- You must be logged in to reply to this topic.