3 Level Sub Navigation with Master Page in List
-
I needed to have a sub-navigation for a WordPress template I am developing, and after searching the entire WordPress Codex, and what seems like the entire Internet, I came up with the following solution:
Requirements
- Has to include the top parent page title and link (if the page is in the about section, I needed to include the about section title and link in the side navigation).
- Lists child pages only 1 level deep, and stays that way on even 3+ level sub pages (Only tested 3rd level pages).
- No plugin required.
Solution
<?php if(!$post->post_parent){ // will display the subpages of this top level page $master = wp_list_pages("title_li=&include=".$post->ID."&child_of=".$post->post_parent."&echo=0&depth=1"); $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1"); }else{ // diplays only the subpages of parent level //$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&depth=1"); if($post->ancestors) { // now you can get the the top ID of this page // wp is putting the ids DESC, thats why the top level ID is the last one $ancestors = end($post->ancestors); $post_ancestors = get_post_ancestors($post); $master = wp_list_pages("title_li=&include=".array_pop($post_ancestors)."&echo=0&depth=1"); $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0&depth=1"); // you will always get the whole subpages list } } if ($children) { ?> <ul> <?php echo $master . $children; ?> </ul> <?php } ?>
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘3 Level Sub Navigation with Master Page in List’ is closed to new replies.