I have written a function for a multilevel wordpress menu, but I’d like it to work for any number of levels, at the moment it is written to work for 3 levels.
//only gets the top level items
$top_level_pages = get_pages('parent=0&sort_column=menu_order&exclude=129,2,13');
foreach($top_level_pages as $page){
//print_r($top_level_pages);
$p_id = $page->ID;
// gets all pages and subpages in one array
$children = get_pages("child_of=$p_id&sort_column=menu_order");
$immediate_children = get_pages("child_of=$p_id&parent=$p_id&sort_column=menu_order");
//print_r($immediate_children);
if($children) {
print '<li class="page_item page-item-'.$page->ID.'"><span class="first-level">'.$page->post_title;
print '</span><ul>';
foreach($immediate_children as $child){
$c_id = $child->ID;
//gets a preformatted menu
$grandchildren = wp_list_pages('depth=1&echo=0&parent='.$c_id.'&sort_column=menu_order&title_li=');
if($grandchildren) {
print '<li class="page_item page-item-'.$child->ID.'"><span class="second-level">'.$child->post_title;
print '</span><ul>';
print $grandchildren;
print '</ul></li>';
}
else {
print '<li class="page_item page-item-'.$child->ID.'"><a href="'.get_page_link($child->ID).'">'.$child->post_title.'</a></li>';
}
}
print '</ul></li>';
}
else {
print '<li class="page_item page-item-'.$page->ID.'"><a href="'.get_page_link($page->ID).'">'.$page->post_title.'</a></li>';
}
}
All we need to do to make your function recursive is move most of the loop into a recursive function:
The hard part is making the “first-level”, “second-level” stuff work. I’d just change it “level-1”, “level-2”, etc. And then you can just start with
$x = 1;
and pass$x+1
each time you call the function recursively.Something like this should probably do the trik (written in the stackoverflow textbox and untested).
used like: