• Hi, I think there must be something fundamental wrong with my understanding of wordpress menus, which is a tad embarrassing. I am using register_nav_menu to register three menu locations in my theme, primary, secondary and subsidiary.

    Then, because I am trying to use SMACSS, I am using a filter on nav_menu_css_class to add extra css classes to menu items based on menu location:

    function mon_add_classes_to_menus( $classes, $item ) {
        $menu_locations = get_nav_menu_locations();  
    
        if ( has_term( $menu_locations['primary'], 'nav_menu', $item) ) {
    
            // add 'menu-primary-item' to each primary menu item
            $classes[] = 'menu-primary-item';
    
        } elseif ( has_term( $menu_locations['secondary'], 'nav_menu', $item) ) {
    
            // add 'menu-secondary-item' to each primary menu item
            $classes[] = 'menu-secondary-item';
    
        } elseif ( has_term( $menu_locations['subsidiary'], 'nav_menu', $item) ) {
    
            // add 'menu-subsidiary-item' to each primary menu item
            $classes[] = 'menu-subsidiary-item';
        };
        return $classes;
    }

    This works fine if I have only the primary menu set in appearance->menus, and also works fine if I have all three set. Or, indeed, the first two. But if I only allocate menus to primary and subsidiary the subsidiary menu gets treated as if it were the secondary menu. So WordPress seems to be allocating the menus to locations in order, ignoring the location chosen in the admin area? Except that the correct template is being used…

    I really don’t want to add a dummy menu to get over this, and I really do want to be able to add extra classes to my menus based on their location, where that location is the one chosen in the admin screen, i.e. relates to physical location in the theme template.

    Can somebody enlighten me as to what I am misunderstanding?

    Thank you!

  • The topic ‘Problem filtering nav_menu_css_class’ is closed to new replies.