0

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?

1
  • 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
    Commented Feb 27, 2013 at 15:43

2 Answers 2

0

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

3
  • 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 Commented Feb 27, 2013 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) Commented Feb 27, 2013 at 17:55
  • You’re probably right—it’s just because I’d like to keep the code as clean as possible. Commented Feb 27, 2013 at 17:56
0

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')) : '';
}

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.