Select Page

Do you want to add a custom CSS class to your navigation menus? You can easily do that using this code.

All you have to do is take this code and put it in your functions.php file:

function add_custom_css_class_nav_menu($classes, $item){
	$classes[] = “my-custom-class”;	 
	return $classes;	 
}	 
add_filter(‘nav_menu_css_class’, ‘add_custom_css_class_nav_menu’, 10, 2);

The above code adds a new filter function called add_custom_css_class_nav_menu to the nav_menu_css_class filter. This filter lets you filter/modify the classes that will be added to all the menus.

If you want you can add conditions for which pages get the class like this:

<?php
function add_custom_css_class_nav_menu($classes, $item){
    if( is_page('my-page-slug') ){
         $classes[] = “my-custom-class”;
    }
    return $classes;
}
add_filter(‘nav_menu_css_class’, ‘add_custom_css_class_nav_menu’, 10, 2);