Recursive menu tree function

This snippet displays a menu tree of pages and the current pages children for up to two levels. How can I make this recursive to work for unlimited levels?

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; ?>

Related posts

Leave a Reply

1 comment

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