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.

Sorry if my post duplicate but i can't find best solution for my problem. I'm newbie on develop WP theme. Now i'd like to add class to current li and last li, but i don't know how to do? Hope someone help.

Here is my HTML code:

<nav>
    <ul>
        <li><a href="#" class="selected">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#" class="last">Contact</a></li>
    </ul>
</nav>
share|improve this question

1 Answer 1

Wordpress does in fact add a current class by default:

check where it says:

<li id="menu-item-1688" class="current-menu-item">

Within your source code.

Now as for the other item, this is a bit tricky.

There is two ways to do it assuming that you need the last item for styling.

Option #1 is with pure CSS using the last-of-type selector.

Option #2, is using the get_wp_nav_menu as opposed to wp_nav_menu and once you are inside the foreach, you compare it using count and the current index of said foreach to add the css class dinamically.

Edit: If you are a bit new to Wordpress theming, I would definitely go with option number 1 despite not having full browser coverage.

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.