WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a static main menu, but I'd like to add the class 'active' to the menu item in the nav if it is the page, or a child of that page.

I imagine it to be a series of something like: if ( $post->post_parent == '1' ) { add_class 'active' }

Can this be done with Wordpress?

            <a class="item" href="/index.php">
                <i class="home icon"></i> Home
            <a class="item" href="/about-us">
                <i class="doctor icon"></i> About Us
            <a class="item" href="/how-do-i">
                <i class="help icon"></i> How Do I
            <a class="item" href="/services">
                <i class="info icon"></i> Our Services

            <a class="item" href="/do-it-online">
                <i class="user icon"></i> Do It Online
            <a class="item" href="/contact-us">
                <i class="mail icon"></i> Contact Us
share|improve this question
You are looking for is_page. But can you explain why you hardcode the menu? – kraftner Mar 18 '14 at 17:25
Partly lack of knowledge of how to do it any other way, partly because I wanted to have the different icons for each page. If you can suggest a better way, I'm all ears. – Josh Mar 18 '14 at 17:31

What you'll need to do is use get_ancestors(). Pass get_ancestors() your current page ID, and then search the resulting array for the menu item ID. If the menu item ID is in the array, set the class to active.

share|improve this answer

Your Answer


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.