Programmatically set current-menu-item using wp_nav_menu

I’m calling wp_nav_menu programmatically on a class and I’d like to be able to set the field that will be shown as the current-menu-item but I don’t see an argument for that.

It seems like I may be able to use nav_menu_css_class?

1 Answer
1

Adding Conditional Classes to Menu Items

This example would let you add a custom class to a menu item based on the condition you specify. Don’t forget to change the condition.

<?php
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
     if(is_single() && $item->title == "Blog"){ //Notice you can change the conditional from is_single() and $item->title
             $classes[] = "special-class";
     }
     return $classes;
}
?>

Also take a look at How to Add a Custom Class to a WordPress Menu Item.

Leave a Comment