Support » Themes and Templates » Adding a class to specific items on a wp menu

Viewing 7 replies - 1 through 7 (of 7 total)
  • Please.. nobody? 🙂

    esmi

    (@esmi)

    Forum Moderator

    We don’t know what the problem is. You need to provide more details.

    Okay!

    I have a old theme created back in 2008 and now I’m trying to “refresh” it adding all new WP features and hooks. For example the theme is not menu-compatible and I’m adding support for it. I managed to do this and all works properly. The only problem I have is that the old menu had some items with a specific class, for example:

    <ul id="nav">
    <li class="separator"><a href="">Title</a></li>
    <li class="separator"><a href="">Title</a></li>
    <li><a href="">Title</a></li>
    </ul>

    Now the new menu is:

    <ul class="menu" id="menu-default">
    <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-15052" id="menu-item-15052"><a href="">Title</a></li>
    <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-15056" id="menu-item-15056"><a href="">Title</a></li>
    <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-15057" id="menu-item-15057"><a href="">Title</a></li>
    </ul>

    How can I have for example 2 of these items with a class I specify and the other with the “default” class?

    I hope it’s more clear now!! Thanks in advance! 🙂

    esmi

    (@esmi)

    Forum Moderator

    Try:

    add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
    function special_nav_class($classes, $item){
         if( $item->ID != '15057') $classes[] = 'separator';
         return $classes;
    }

    No luck (the class for the item 15057 is still menu-item menu-item-type-post_type menu-item-object-page menu-item-15057)… 🙁
    If you need the url, it’s http://bombacarta.danielamandolini.it

    WP latest version

    esmi

    (@esmi)

    Forum Moderator

    My guess is that you’re going about this the wrong way. You’re using the extra class to add separators to your menu items, yes? If so, you can achieve exactly the same thing using the CSS that WP generates.

    Ops… you are right… 😛

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Adding a class to specific items on a wp menu’ is closed to new replies.