递归菜单树功能

时间:2012-07-20 19:53:03

标签: php recursion wordpress

此代码段显示一个页面菜单树,当前页面显示最多两个级别的子页面。如何让这个递归工作无限级别?

if( empty($wp_query->post->post_parent) ) {
    $parent = $wp_query->post->ID;
} else {
    $parent = $wp_query->post->post_parent;
} ?>
<?php if(wp_list_pages("title_li=&child_of=$parent&echo=0" )): ?>
<div id="submenu">
    <ul>
    <?php wp_list_pages("title_li=&child_of=$parent" ); ?>
    </ul>
</div>
<?php endif; ?>

1 个答案:

答案 0 :(得分:0)

<?
function getParent($obj){
  if (empty($obj->post->post_parent)){
    return $obj->post->ID;
  } else {
    return getParent($obj->post->post_parent);
  }
}
$parent = getParent($wp_query)
?>