<?php
/**
* Multi-level pages menu
*/
function wptt_multilevel_menu() {
global $post;
// Top level menu is always displayed
$top_level = wp_list_pages('title_li=&depth=1&sort_column=menu_order&echo=0');
// Get post ancestors
$post_ancestors = get_post_ancestors($post);
// Check if a page has any parent pages
if ($post_ancestors) {
//get the top page id
$top_page = $post_ancestors ? end($post_ancestors) : $post->ID;
// How many ancestors does this page have? Counts the array adds one.
$n = count($post_ancestors) + 1;
// Get the pages children, if it has any
$pages = get_pages();
$page_children = get_page_children($post->ID, $pages);
// Checks if a page has children
if (!empty($page_children)) {
$children = wp_list_pages("title_li=&child_of=". $top_page ."&echo=0&sort_column=menu_order&depth=" . $n);
} else { // If the page doesn't have children
$children = wp_list_pages("title_li=&child_of=". $top_page ."&echo=0&sort_column=menu_order&depth=" . ($n - 1));
}
} else {
$children = wp_list_pages("title_li=&child_of=". $post->ID ."&echo=0&sort_column=menu_order&depth=1");
}
// Put it all together
$menu = '<ul class="menu top_level">';
$menu .= $top_level;
$menu .= '</ul>';
// Only show child navigation if there are children
if ( $children ) {
$menu .= '<ul class="menu subpages">';
$menu .= $children;
$menu .= '</ul>';
}
print $menu;
}
?>
Thanks to Curtis Henson for the above code