1

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>
            <a class="item" href="/about-us">
                <i class="doctor icon"></i> About Us
            </a>
            <a class="item" href="/how-do-i">
                <i class="help icon"></i> How Do I
            </a>
            <a class="item" href="/services">
                <i class="info icon"></i> Our Services
            </a>

            <a class="item" href="/do-it-online">
                <i class="user icon"></i> Do It Online
            </a>
            <a class="item" href="/contact-us">
                <i class="mail icon"></i> Contact Us
            </a>
        </nav>
  • 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
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.