WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding a class to specific items on a wp menu (8 posts)

  1. MultiformeIngegno
    Member
    Posted 2 years ago #

    Hello, as the title says, I'm trying to add a class for certain items on a wp menu I created through the built in menu function.

    I read http://codex.wordpress.org/Function_Reference/wp_nav_menu#Adding_Conditional_Classes_to_Menu_Items but I couldn't understand how to obtain the result... can you help me?

    Thanks in advance!

  2. MultiformeIngegno
    Member
    Posted 2 years ago #

    Please.. nobody? :)

  3. esmi
    Forum Moderator
    Posted 2 years ago #

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

  4. MultiformeIngegno
    Member
    Posted 2 years ago #

    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! :)

  5. esmi
    Forum Moderator
    Posted 2 years ago #

    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;
    }
  6. MultiformeIngegno
    Member
    Posted 2 years ago #

    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

  7. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  8. MultiformeIngegno
    Member
    Posted 2 years ago #

    Ops... you are right... :P

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags