Created a sidebar that lists WP's default pages' children. Yet there is some intermittent behavior where the sidebar lists the directory correctly on one page, but nothing on another.

Live examples where it works fully can be seen here; where nothing appears on the ancestor page here; and where the ancestor sidebar heading appears, but not the child pages.

Because it's working on some pages but not others, I'm really unsure what is going on. They are all the same template and calling the same navigation.

My code is below:

<?php ( is_page() ) { 
    global $post;
    $ancestors = get_post_ancestors( $post->ID ); // Get the array of ancestors

    //Get the top-level page slug for sidebar/widget content conditionals
        $ancestor_id = ($ancestors) ? $ancestors[count($ancestors)-1]: $post->ID;
        $the_ancestor = get_page( $ancestor_id );
        $ancestor_slug = $the_ancestor->post_name;

     //If there are no ancestors display a menu of children
        if (count($ancestors) == 0 && is_front_page() == false || is_page('hammond-society') ) {
                $page_name = $post->post_title;
                $test_menu = wp_nav_menu( array( 
                    'theme_location' => 'main_nav', 
                    'menu_class' => 'nav',
                    'container_class' => 'offset-gutter',
                    'items_wrap' =>  '<div class="radius-topright" id="sidebar_header"><h5 class="white">Also in <span class="grey bold">' . $page_name . '</span></h5></div><ul class="%2$s">%3$s</ul>',               
                    'submenu' => $page_name,
                    'depth' => 1,
                    'echo' => false
            if (strpos($test_menu,'<li id') !== false) : echo $test_menu; endif;

    //If there are one or more display a menu of siblings
        elseif (count($ancestors) >= 1) {
            $parent_page = get_post($post->post_parent);
            $parent_url = $parent_page->guid;
            $parent_name = $parent_page->post_title;

            <div class="offset-gutter radius-topright" id="sidebar_header">
                <h5 class="white">Also in <a href="<?php echo $parent_url;?>" class="grey bold"><?php echo $parent_name ?></a></h5>
            wp_nav_menu( array( 
                'theme_location' => 'main_nav', 
                'menu_class' => 'nav', 
                'container_class' => 'offset-gutter',
                'submenu' => $parent_name,
                'depth' => 1                

1 Answer 1


Hate to answer my own question, but I fixed it.

Seems the issue was with how the pages were getting added to the menu via the WordPress admin. After removing and adding them back, the sidebars appeared just fine. So, in the event someone else has a similar problem, just delete the pages and re-add them.

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.