drngnh708353 2011-12-11 19:03
浏览 52
已采纳

函数不返回“functions.php”中的值

I have created a function within my themes functions.php file in order to persist a list of child pages from the level of a parent page while navigating deeper within the hierarchy.
My issue is that when I call the function within the page.php theme file I dont get anything back.

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(); ?>
  • 写回答

1条回答 默认 最新

  • doubo7131 2011-12-11 19:09
    关注

    I guess you forgot the second return. You have an if with two code paths, but only return a value in the last one. You need:

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

    Your function is (recursively) calling itself when that condition matches. But you still need to instruct PHP that it should pass on the value to the outer call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看