I am trying to show a sub-menu of child-pages only on level 2 pages (who have a parent) and if they have children. I am using the code below (made from several snippets I found), it works fine on level 1 (not showing anything as meant to be), works fine on level 2: if there are child pages they are shown in the sub-menu.
My question: When i go to level 3 pages I would like the sub-menu to stay but it disappears. How can I change the code below to make it work?
<?php
if (is_page() && $post->post_parent ) { // test to see if the page has a parent
$page = $post->ID;
if (is_page() ) {
$page = $post->post_parent;
}
$children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' );
if ($children) {
$output = wp_list_pages( array(
'child_of' => $post->ID, // Only pages that are children of the current page
'depth' => 1, // Only show one level of hierarchy
'title_li' => ''
) );
}
}
echo $output;
?>
Use ancestors: