Site logo

Archived topic

How to set menu choice as current in certain pages

32 replies · Started by Harris on October 2, 2017

Viewing posts 16–30 of 33

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.

Thanks Tom. That works fine!
Can it somehow be extended in order to cover the category archive pages too?

Thanks
Harris

Sure, 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 );

Thanks Tom. Perfect!

You're welcome :)

Hello

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

Hi there,

is_singular() takes the actual post type slug, so yours would be:

is_singular( 'koupi_events' )

Thanks a lot Tom.

Its working.

I thought it was taking the post type name (and not slug)

Glad I could help! :)

Hi, 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/

Hi 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.

And is there any relationship between: Päättäjille and tule-kustannukset ?

No ”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)

This archived topic is closed to new replies.