Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This code adds an extra class to all my menu items:

add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
  $classes[] = 'btn';
  return $classes;
}

How do I confine this filter to my main menu (in theme location 'primary-menu')?

Regards,

Daniel

share|improve this question
add comment

2 Answers

I came across this thread trying to solve the same problem - this is what I've come up with. I don't know how well this performs, since it'll be called for every single menu item, but it seems menus are set up as taxonomies inside WordPress, and so you can use has_term() to determine if the item is in a particular menu, and get_nav_menu_locations() to pull back the list of which menus are in which theme location.

Modifying your code:

add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
    $menu_locations = get_nav_menu_locations();
    if ( has_term($menu_locations['primary-menu'], 'nav_menu', $item) ) {
        $classes[] = 'btn';
    }
    return $classes;
}
share|improve this answer
add comment

You don't need to modify your functions.php. Just go into your template file and find wp_nav_menu and add 'menu_class' to it.

<?php wp_nav_menu( array( 'theme_location' => 'primary','menu_class' => 'newmenuclass' ) ); ?>
share|improve this answer
    
Thanks for the response but I need a class for the menu items (li), not the whole menu (ul). –  Daniel Baars Mar 13 '13 at 16:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.