This code adds an extra class to all my menu items:
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
$classes[] = 'btn';
return $classes;
}
How do I confine this filter to my main menu (in theme location ‘primary-menu’)?
Regards,
Daniel
4 s
I was also trying to solve this problem and while searching for a solution, discovered that the nav_menu_css_class
filter actually excepts a third parameter. This is an object containing variables related to your menu and includes the theme_location
variable you can use to conditionally apply class names to a specific menu. You just need to ensure that you set the theme_location
when you call wp_nav_menu in your theme.
Here’s an example:
add_filter( 'nav_menu_css_class', 'special_nav_class', 10, 3 );
function special_nav_class( $classes, $item, $args ) {
if ( 'primary-menu' === $args->theme_location ) {
$classes[] = 'btn';
}
return $classes;
}