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

Im writing a customWalker for the wordpress main navigation area.

But i am a little stuck on how to find and add a class to the parent of the child which is active.

For instance

I have the whole customwalker working perfect, all parents have a active class assigned when they are active, but how to I assign an active class to the parent when a sub class is active?

I managed to assign a active class to the top depth when current_page_item was given like this:

if($depth === 0){
            $class_names = str_replace('current_page_item', 'active', $class_names);

But how would I write it for a child class to show the parent an active when the child is active?


share|improve this question
Have you seen this answer? The first part of the code solves your problem. – toscho Jul 22 '13 at 12:01
@toscho This just asks if has children, I have this already, I am wanting to know the logic to add a class to the parent when the child is active. Thanks though :) – Robert Jul 22 '13 at 12:26
up vote 0 down vote accepted

By default, WordPress applies ancestor classes to the parents of active items. All you have to do is target them via CSS:

.nav-menu li.current_page_ancestor > a,
.nav-menu li.current-menu-ancestor > a {}

This code sample implies that your menu has the .nav-menu class. This will work with a navigation menu and with the page menu fallback.

share|improve this answer
Thank you, I did not know this :) – Robert Jul 22 '13 at 14:28

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.