Nested Subnavigation

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');

	}
}

Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.