1

Just playing around with adding "custom menu" support to my theme. I'm super excited about it.

HOWEVER,

Check this out >

<ul id="menu-my-main-menu" class="menu">
  <li id="menu-item-12" 
      class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-12">
      <a href="http://localhost/bizsite">Home</a>
  </li>
</ul>

Worst case of "class-itus" I think I've ever seen. Is this something "I" did unintentionally or is this the default?

How can I control or remove the class attribs on these menus?

4
  • 1
    Worst "class-itus?" You haven't seen Drupal then. ;-) BTW, is there a functional reason you want to remove them or do you just dislike them? Mar 5, 2011 at 16:44
  • @Mike: You're right. I've no need for Drupal with WP ;-)
    – Scott B
    Mar 6, 2011 at 4:03
  • @Mike: I'm all for using relevant class attribs, however in this case, there's no need for anything beyond maybe a single declaration on the li elements and that would be a single current-menu-item IMHO. List items are structurally self describing, so I'm not sure why we need this level of detail. I can get virtually any design I need (including flyout menus) with the parent ul class and nothing more. There's nothing significantly different about them to warrant 7 class names on a single li element. You'd have to agree, no? (crosses his fingers that Mike didn't write these bits into core :-)
    – Scott B
    Mar 6, 2011 at 4:15
  • LOL! I've written nothing into core. I understand you don't need them, but I wonder if it's worth the effort to remove them when it will take more code and slight more processing time to get rid of them. Why not focus on adding things that have benefit instead of removing things that have no affect? I'm just sayin... Mar 6, 2011 at 6:43

1 Answer 1

5

You can use the nav_menu_css_class filter to remove the classes that you want from your menu-item.

See the example below, to remove ALL the CSS classes that are appended to you menu items:

add_filter('nav_menu_css_class','remove_nav_menu_classes');
function remove_nav_menu_classes($classes) {
    return array(); 
}

Do a print_r($classes) if you want to know which classes are active in your menu.

5
  • You need to return on filter callbacks, i'll +1 the answer if you add that in.
    – t31os
    Mar 5, 2011 at 14:37
  • @keatch: Thanks for the answer. I'll be putting this to immediate use :-)
    – Scott B
    Mar 6, 2011 at 4:07
  • @keatch: is this all or nothing? The one thing I would like to retain is the current-menu-item which is actually the only usable class I can see a need for here.
    – Scott B
    Mar 6, 2011 at 4:12
  • You can leave it as the only item and unsetting all the others elements. $classes is an array that you can iterate over
    – keatch
    Mar 6, 2011 at 17:37
  • The filter should return an array, otherwise join() will emit a warning because it is called on NULL. I've updated the answer.
    – Jan Fabry
    May 4, 2011 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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