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

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

I've created a menu for each section of the site, and I'd like the sidebar to only show the relevant menu to pages in a section, so the 'About' page and its children receive the about menu and the 'Advice' page and its children the advice menu, and so on and so forth.

I've got this working for parent pages but I can't get it working with their children. I've tried various suggestions from around the internet but nothing seems to work correctly.

This code:

<?php if(is_page('advice')){
        wp_nav_menu( array( 'theme_location' => 'advice-menu' ) );
} ?>

gets the correct menu in the correct section, but I need to extend it to cover child pages as well.

All menus are properly registered in both functions.php and Appearance > Menus and both the parent and child pages are all using the same page template.

share|improve this question
up vote 0 down vote accepted

Bainternet's solution will definitely work, but it you want to use the page slug instead of ID you could put this in your functions.php file:

function get_ID_by_slug($page_slug) {
    $page = get_page_by_path($page_slug);
    if ($page) {
        return $page->ID;
    } else {
        return null;

function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
    global $post;         // load details about this page
    $pid = get_ID_by_slug($pid);
    if( is_page() && ($post->post_parent==$pid || is_page($pid) ) )
               return true;   // we're at the page or at a sub page
               return false;  // we're elsewhere

And then use this for the conditional statement in your sidebar.php or whatever:

<?php if ( is_tree('advice') ) { 
     wp_nav_menu( array( 'theme_location' => 'advice-menu' ) );
} elseif ( is_tree('another-page-slug') ) { 
     wp_nav_menu( array( 'theme_location' => 'another-page-menu' ) );
} ?>

That will return any page that is within the 'tree' - both the parent page itself and any children below it. Hope this helps, best of luck!

share|improve this answer
That works perfectly! Thanks a lot :) – TheGuest Mar 21 '12 at 21:09

I have a function i made a while a go that will help you:

 * Function to check if the current page/ custom is a child of a given one
 * @param (int) - parent id to check
 * @return (bool) - if it is a child true else it returns false
function is_child_of($parent_id) {
    global $post;
    $ps = get_post_ancestors( $post );
    if (empty($ps)) return false;
    return (in_array($parent_id,$ps));

The only down side is that you must know the parent page's ID, if not the I can extend it a bit to work with page slug as well.

Anyway once you have this function in place you can change your conditional check to something like this:

<?php if(is_page('advice') || is_child_of(12)){
        wp_nav_menu( array( 'theme_location' => 'advice-menu' ) );
} ?>

assuming that the id of advice is 12 :)

share|improve this answer

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.