0

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>

1 Answer 1

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.

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.