-1

i have a problem. I have an CSS Dropdown Menu. The HTML Code of the is this:

<ul id="nav">
  <li><a href="#">Home</a></li>
  <li><a href="#">Archiv</a></li>
  <li><a href="#" class="sub-nav">Kontakt</a>
    <ul>
      <li><a href="#">Submenue 1</a></li>
      <li><a href="#">Submenue 2</a></li>
      <li><a href="#">Submenue 3</a></li>
    </ul>
  </li>
  <li><a href="#">Impressum</a></li>
</ul>

WordPress makes them into the following:

<ul id="nav" class="menu">
    <li id="menu-item-636" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-636">
    <li id="menu-item-614" class="sub-nav menu-item menu-item-type-post_type menu-item-object-page menu-item-614">
    <li id="menu-item-615" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-615">
        <a href="http://welitso.de/2010?preview=1&template=welitso_html5&stylesheet=welitso_html5" title="archiv">Archiv</a>
        <ul>
            <li><a href="#">Submenue 1</a></li>
        </ul>
    </li>
    <li id="menu-item-1013" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1013">
</ul>

How can i tell WordPress that the link on "menu-item-615" should have the a-class "sub-nav" ?

3
  • 1
    I posted an answer to an almost identical question here. Use that code, and change 'has_children` with your sub-nav class. Dec 18, 2011 at 18:39
  • Thank you. Now i get the class in the "li" element. How can i put this as a link class to the link in this li element? --> <li><a href="#" class="sub-nav">Entry 1</a></li>
    – hazelnut
    Dec 20, 2011 at 19:48
  • You don't need it on the link; just use ... li.sub-nav > a when selecting the element Dec 23, 2011 at 12:28

1 Answer 1

1

This is tested, this will take all your sub nav and add the class.

$('#nav li > ul').parent().children("a").addClass("sub-nav");
8
  • Thanks but i think that adds an class to the ul. I need it in a link tag.
    – hazelnut
    Dec 18, 2011 at 17:59
  • try my edited code, you can just add an a after the class selector Dec 18, 2011 at 18:00
  • That would only affect on this link (615)? If I add more such links, I would have to repeat this ever and ever. Is there any possibility, menu entries with a sub-menu provide with a class?
    – hazelnut
    Dec 18, 2011 at 18:06
  • check my edit, now the above code will apply on all the sub menu only if it has Dec 18, 2011 at 18:12
  • Thanks! :) But i dint understand how should i past this? In the wp_nav_menu code?
    – hazelnut
    Dec 18, 2011 at 18:26

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.