Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to highlight the parent of a child page in the menu when the child page itself is not in the menu.

I know this would work if you add the child as a sub page but that isn't the case.

Any ideas?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Alrdy got it:

<?php //in functions.php
add_filter('nav_menu_css_class', 'highlight_portfolio', 12, 2);
function highlight_portfolio($classes, $item) {
    $parent = get_post_ancestors();
    $parent_ID = $parent[0];

    if ($parent_ID == $item->object_id) {
        array_push($classes, 'current-menu-ancestor');
    return $classes;
share|improve this answer

As an alternative to @janw's answer, you can insert the following code in the theme’s footer.php file right before the closing body tag .

<!-- Highlight parent page link when on child page -->
<?php if (is_page()) {   //  displaying a child page ?>
    <script type="text/javascript">
<?php } ?>

I've detailed how this code works in a post here: How to keep parent page navigation link highlighted when viewing a child/sub page!

share|improve this answer
That would work. But why would you want that if; 1) php is faster and 2) more reliable js can be disabled. 3) (nitpick) increases page load. –  janw Feb 18 at 18:44
@janw Thanks for the feedback mate. I tried your solution but I kept getting this error: I'm nowhere good with PHP, but I tried changing the string 'current-menu-ancestor' to 'current-page-ancestor' then to 'current-menu-item' (that might have just made it more wrong) to no avail. Here's how the code looks on my functions.php file: Appreciate your help, and love your solution (wish I could get it to work on mine)! –  Bibiano Wenceslao Feb 21 at 1:51

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.