Add class to menu items of one specific menu (nav_menu_css_class)

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;
}

Leave a Comment