0
votes

We are working with Genesis theme for WordPress and want to add some classes to the menu. We did some search online and found this code:

// Add a CSS class to a menu or menu item in WordPress
function modify_nav_menu_args( $args) {
        $args['menu_class'] .= ' genesis-nav-menu js-superfish sf-js-enabled sf-arrows'; 
        return $args;
}
add_filter( 'wp_nav_menu_args', 'modify_nav_menu_args' );

Works great only the problem is that we also have a menu in the footer and this menu is getting those classes to.

We are not using "primary" of "secondary" menu's for loading the menu's but we do it through widgets.

How can I use above function and add an if statement to it for saying that I only want to add this css to the menu with the name "main menu" and ID "11". If not "main menu", or if you want to do it by ID "11", then add other classes.

I allready found something like this:

function modify_nav_menu_args( $args ) {
 if( 'primary' == $args['theme_location'] ) {
 $args['menu_class'] .= ' special-class';
   }
return $args;
}
add_filter( 'wp_nav_menu_args', 'modify_nav_menu_args' );

But then you use "primary" of the theme_location and we are not using "primary" or "secondary".

Somebody can help me out how I can do this with menu name or ID?

Var_dump $args

array(17) { ["menu"]=> object(WP_Term)#21050 (11) { ["term_id"]=> int(11) ["name"]=> string(9) "main menu" ["slug"]=> string(9) "main-menu" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(11) ["taxonomy"]=> string(8) "nav_menu" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(28) ["filter"]=> string(3) "raw" ["meta"]=> array(0) { } } ["container"]=> string(3) "div" ["container_class"]=> string(0) "" ["container_id"]=> string(0) "" ["menu_class"]=> string(4) "menu" ["menu_id"]=> string(0) "" ["echo"]=> bool(true) ["fallback_cb"]=> string(0) "" ["before"]=> string(0) "" ["after"]=> string(0) "" ["link_before"]=> string(0) "" ["link_after"]=> string(0) "" ["items_wrap"]=> string(36) "
%3$s
" ["item_spacing"]=> string(8) "preserve" ["depth"]=> int(0) ["walker"]=> string(0) "" ["theme_location"]=> string(0) "" }
1

1 Answers

1
votes

The $args variable should have what you need already.

function modify_nav_menu_args( $args ) {
    if ($args['menu'] instanceof WP_Term && 'main-menu' == $args['menu']->slug) {
        $args['menu_class'] .= ' genesis-nav-menu js-superfish sf-js-enabled sf-arrows';
    }
    return $args;
}