Add class active to static menu

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’ }

Read More

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>

Related posts

Leave a Reply

1 comment

  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.