Make a php function recursive

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>';
}
} 

Related posts

Leave a Reply

2 comments

  1. 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.

  2. Something like this should probably do the trik (written in the stackoverflow textbox and untested).

    function listChildren($parentID, &$menu)
    {
        static $options = 'parent=0&sort_column=menu_order&exclude=129,2,13';
        static $level = 1;
    
        foreach(get_pages(sprintf('child_of=%d&%s', $parentID, $options ) as $page)
        {
            $menu .= sprintf('<li class="level-%d"><a href="%s">%s</a>', 
                        $level
                        get_page_link($page->ID),
                        $page->post_title);
    
            if ($page->hasChildrenOrWhateverWPUses())
            {
                $level ++;
                $menu .= sprintf('<ul class="menu-level-%d">', $level);
                listChildren($page->ID, $menu);
                $menu .= '</ul>';
                $level --;
            }
    
            $menu .="</li>"
        }
    }
    

    used like:

    $menu =  '<ul class="menu-tree">';
    listChildren($rootPageID, $menu);
    $menu .= '</ul>';
    
    echo $menu;