2011-12-11 83 views
2

我已經在我的主題functions.php文件中創建了一個函數,以便在層次結構中更深入地瀏覽父頁面級別時保留子頁面的列表。
我的問題是,當我在page.php主題文件中調用函數時,我沒有收到任何迴應。函數不返回值在「functions.php」

的functions.php:

function get_top_parent_children($the_id,$start = 0,$walk = null){ 

    $current_post = get_post($the_id); 
    $walk[] = $current_post; 

    if($current_post->post_parent){ 
     get_top_parent_children($current_post->post_parent,$start,$walk); 
    } else { 
     $output = array_reverse($walk); 
     return get_children($output[$start]->ID); 
    } 
} 

page.php文件:

<?php get_header(); ?> 
<div id="primary" class="clearfix"> 

    <div id="content"> 
     <?php the_post(); ?> 
     <?php $children = get_top_parent_children($id,1); ?> 
     <?php if ($children) { ?> 
      <section id="post-topic"> 
       <h2><?php the_title() ?> Topics</h2> 
       <ul class="clearfix"> 
        <?php foreach($children as $child){ ?> 
         <li> 
          <a href="<?php echo get_permalink($child->ID) ?>"> <?php echo child->post_title ?> </a> 
         </li> 
        <?php }; ?> 
       </ul> 
      </section> 
     <?php }; ?> 
     <?php get_template_part('content', 'page'); ?> 
    </div> 
    <aside> 
     <?php dynamic_sidebar('Page Sidebar'); ?> 
    </aside> 
</div> 
<?php get_footer(); ?> 
+0

你需要附上get_children – ajreal

回答

4

我想你忘了第二return。您有一個if帶有兩個代碼路徑,但只返回最後一個值。您需要:

if($current_post->post_parent){ 
    return get_top_parent_children(...); 
    # ^^^^ 
} else { 
    return get_children(...); 
} 

您的函數(遞歸)在條件匹配時調用自己。但是您仍然需要指示PHP應該將值傳遞給外部調用。