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>
What you’ll need to do is use
get_ancestors()
. Passget_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.