[Resolved] How to set menu choice as current in certain pages

Home Forums Support [Resolved] How to set menu choice as current in certain pages

Home Forums Support How to set menu choice as current in certain pages

Viewing 15 posts - 16 through 30 (of 33 total)
  • Author
    Posts
  • #1106336
    Tom
    Lead Developer
    Lead 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.

    #1106461
    Harris

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

    Thanks
    Harris

    #1107050
    Tom
    Lead Developer
    Lead Developer

    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 );
    #1107091
    Harris

    Thanks Tom. Perfect!

    #1107358
    Tom
    Lead Developer
    Lead Developer

    You’re welcome 🙂

    #1286300
    Harris

    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

    #1286304
    Harris
    #1286529
    Tom
    Lead Developer
    Lead Developer

    Hi there,

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

    is_singular( 'koupi_events' )

    #1286823
    Harris

    Thanks a lot Tom.

    Its working.

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

    #1287584
    Tom
    Lead Developer
    Lead Developer

    Glad I could help! 🙂

    #2497262
    Kirmo

    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/

    #2497351
    David
    Staff
    Customer Support

    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.

    #2497376
    Kirmo

    wasn’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.

    #2497379
    David
    Staff
    Customer Support

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

    #2497387
    Kirmo

    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)

Viewing 15 posts - 16 through 30 (of 33 total)
  • You must be logged in to reply to this topic.