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?


  • Have you seen this answer? The first part of the code solves your problem.
    – fuxia
    Jul 22, 2013 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, 2013 at 12:26

1 Answer 1


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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