July 12, 2017
Use with Link to Top-most Page in Hierarchy for a more dynamic nested sub-navigation menu.
Original:
global $post;
$ancestors = get_post_ancestors($post->ID);
if (is_page()) {
$page = $post->ID;
if ($ancestors[0] == 6 && $post->post_parent) { // ID of page with 3rd level navigation; repeat this block as necessary, and update ID with topmost ID of tree
$page = $post->ID;
$children = wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=&depth=1' );
if ($children) {
$output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=&depth=1');
}
} else if ($post->post_parent) { // mid-level or parent navigation
$page = $post->post_parent;
}
$children = wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=&depth=1' );
if ($children) { // all other instances
$output = wp_list_pages('echo=0&child_of=' . $page . '&title_li=&depth=1');
}
}
With Page Ancestor ID:
$top_page_url = get_permalink( end( get_ancestors( get_the_ID(), 'page' ) ) );
global $post;
$ancestors = get_post_ancestors($post->ID);
if (is_page()) {
$page = $post->ID;
if ($ancestors[0] == $top_page_url && $post->post_parent) { // ID of page with 3rd level navigation; repeat this block as necessary, and update ID with topmost ID of tree
$page = $post->ID;
$children = wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=&depth=1' );
if ($children) {
$output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=&depth=1');
}
} else if ($post->post_parent) { // mid-level or parent navigation
$page = $post->post_parent;
}
$children = wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=&depth=1' );
if ($children) { // all other instances
$output = wp_list_pages('echo=0&child_of=' . $page . '&title_li=&depth=1');
}
}