views:

287

answers:

2

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>';
}
} 
+2  A: 

All we need to do to make your function recursive is move most of the loop into a recursive function:

<?php
//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;
    recursiveFunction($p_id);
}

function recursiveFunction($p_id){
    $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) {
            recursiveFunction($child->ID);
        }
        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>';
    }
}
?>

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.

Brendan Long
@Brendan: To make first-level, second-level, etc, I would make an `array('first-level','second-level',[...],'tenth-level');` and then combine that with your `$x+1` suggestion. Then: `$className = array_key_exists($levels,$x) ? $levels[$x] : "level-$x";`
Josh
thanks Brendan, I'll also test this option
paul
+1  A: 
Kris
thanks I will test this and post back if it worked or not
paul