[英]function not returning value in “functions.php”
我已经在主题functions.php
文件中创建了一个函数,以便在层次结构中进行更深层次的导航时,从父页面的级别保留子页面列表。
我的问题是,当我在page.php
主题文件中调用该函数时,我什么也page.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(); ?>
我猜你忘了第二次return
。 您有一个带有两个代码路径的if
,但是仅在最后一个中返回一个值。 你需要:
if($current_post->post_parent){
return get_top_parent_children(...);
# ^^^^
} else {
return get_children(...);
}
当条件匹配时,您的函数正在(递归)调用自身。 但是您仍然需要指示PHP将其值传递给外部调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.