2011-12-11 3 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에 지시해야합니다.

관련 문제