WordPress nav_menu_css_class theme filter is not being called

I’m having problems getting a filter to work. I’m trying to add a class to each element of a menu. This is on my theme’s functions.php file:

/**
* Adds custom classes to the items in the nav menu
*
* @param array $classes Classes for the body element.
* @return array
*/
function nav_menu_item_classes( $classes, $item, $args, $depth ) {

// This is what I used to check that it’s not working
error_log(“is nav_menu_item_classes working?”);

if ( ‘menu-1’ === $args->theme_location ) {
// Add the Bootstrap nav-item class
$classes[] = ‘nav-item’;
}

return $classes;

}

add_filter( ‘nav_menu_css_class’, ‘nav_menu_item_classes’, 10, 4 );

This is how I call the menu on my header.php file:

<?php
wp_nav_menu( array(
‘theme_location’ => ‘menu-1’,
‘menu_id’ => ‘primary-menu’,
‘menu_class’ => ‘navbar-nav’,
) );
?>

Everything seems to match, yet the filter is not executed. What could be wrong? Thanks for your help.

Read more here:: WordPress nav_menu_css_class theme filter is not being called

Leave a Reply

Your email address will not be published. Required fields are marked *