-2
我创建了一个递归函数来确定当前页面是否是页面树中页面的子元素。我的递归函数不起作用
我的功能:
function is_ancestor_of($page_name = null, $post) {
if(is_null($page_name))
return false;
// does it have a parent?
if (!isset($post->post_parent) OR $post->post_parent <= 0)
return false;
//Get parent page
$parent = wp_get_single_post($post->post_parent);
if ($parent->post_name == $page_name){
echo $parent->post_name.' - '.$page_name;
return true;
} else {
is_ancestor_of($page_name, $parent);
}
}
这是我与测试代码:
$is_parent = is_ancestor_of('account', $post);
if(!$is_parent) {
echo '<br/> No match';
}
这是结果:
账户 - 账户
不匹配
这表明它找到父页面名称的“帐户”,但它没有返回任何数据。我也测试过返回一个字符串,但那也行不通。
任何人都可以看到我做错了什么?
这是行不通的。因为你第一次调用它,它会运行到'if($ post-> post_name == $ page_name)'。而且由于它已经递归了两次,它仍然没有返回任何明智的数据。 – Steven 2012-07-15 17:28:32
解决了它。通过删除'if($ post-> post_name == $ page_name)'并添加'return true;'在函数底部解决了它:)感谢提示:) – Steven 2012-07-15 17:33:37