I came across a great post by Curtis Henson called Dynamic Multi-level Page Menus in WordPress that was very helpful in getting the menu to show or hide sub-pages depending on the current page. Unfortunately for me, this is meant for a horizontal menu. I am looking for help modifying the code to meet my needs. I'll explain below. Here's the code Curtis provides:
PHP Code:
<?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;
}
?>
The last section "if ($children)" will add an additional unordered list of child pages underneath the main menu. I'm looking for help in adding that additional <ul> into the main <ul> to create the right structure for a vertical menu, like this:
HTML Code:
<ul class="menu top_level">
<li><a>Parent Page</a>
<ul class="menu subpages">
<li><a>Sub Page</a></li>
</ul>
</li>
</ul>
Bookmarks