函数不返回“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文件中创建了一个函数,以便在层次结构中更深层导航时从父页面级别保留子页面列表。我的问题是,当我在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(); ?>
1 个解决方案
#1
4
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,但只返回最后一个的值。你需要:
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.
当该条件匹配时,您的函数(递归地)调用自身。但是你仍然需要告诉PHP它应该将值传递给外部调用。
更多相关文章
- 在通过AJAX响应发送的页面上执行javascript函数
- php 基本的常用字符串函数
- PHP中使用sleep函数实现定时任务实例分享
- 040-PHP使用闭包函数来进行父实例的变量自增,正确示例
- PHP常用函数归类总结【大全】
- php mail函数一段好的代码
- C#,PHP对应加密函数
- php 无限级分类 递归+sort排序 和 非递归
- php计算几分钟前、几小时前、几天前的几个函数