drzrzzkh462254 2010-03-10 21:47
浏览 49
已采纳

使php函数递归

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条回答 默认 最新

  • dsxay48646 2010-03-10 22:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab