How to add class to menu items of one specific menu (nav_menu_css_class)?
-
I want to highlight my menu when I’m on the current page.
My html of menu goes like this:
<ul class="nav"> <li class="active"><a href="index.html">Home</a></li> <li><a href="#">Programs</a> <ul> <li><a href="#">MBBS </a></li> <li><a href="#">MPH</a></li> <li><a href="#">MPHN</a></li> </ul> </li> </ul>
I made changes on my wp menu as:
wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav', '' => 'container', ) );
On function I added:
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2); function special_nav_class($classes, $item){ if( in_array('current_page_item', $classes) ){ $classes[] = 'active '; } return $classes; }
I’ve checked so many times but couldn’t find out the error why is it not working. I just need to define ‘active’ class for my menu items (li) so that menus get active on their pages.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘How to add class to menu items of one specific menu (nav_menu_css_class)?’ is closed to new replies.