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.

I have a short function to clean up the WP Nav Menu.

add_filter('nav_menu_css_class','strip_classes');

function strip_classes($a){ 
    return (in_array('menu-item-71',$a))? array('contact') : array(); 
}

The class name menu-item-71 of a certain nav point is replaced by contact. Now I’d like a second nav point with a class named menu-item-34 to be renamed to about-me. How is this possible?

share|improve this question
    
If this code works (I haven't tested it) then you can solve the second problem in exactly the same way you solved the first one. –  s_ha_dum Feb 27 '13 at 15:43

2 Answers 2

Why does this need to be done programmatically? WordPress's menu editor lets you manually add a CSS class. If you aren't seeing this option then look in the top, right of your screen for the "Screen Options" and click it so that it expands. Then check "CSS Classes".

Add CSS Classes from Screen Options

Now add your custom class

share|improve this answer
    
Because I’d like to remove all the other (for me) useless classes and IDs that WP adds automatically like menu-item menu-item-type-post_type menu-item-object-page –  user1706680 Feb 27 '13 at 17:49
    
Ok. Was just throwing this out there in case you didn't know about it. The classes might be useless but they aren't really causing any problem. Just seems like you are creating extra work for yourself (says the queen of creating extra work for herself) –  helgatheviking Feb 27 '13 at 17:55
    
You’re probably right—it’s just because I’d like to keep the code as clean as possible. –  user1706680 Feb 27 '13 at 17:56

With helgatheviking’s advice in mind giving the nav items speaking class names I came across this solution, with about-me and contact being the two classes that won’t be removed.

add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);

function my_css_attributes_filter($var) {
    return is_array($var) ? array_intersect($var, array('about-me', 'contact')) : '';
}
share|improve this answer

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.