我正在尝试创建一个简单的函数来进行“状态测试”。目标是测试并查看正在查看的当前页面是否有任何子页面。使用它来改变布局以适应子页面。以下代码似乎应该可以工作,但唉,没有骰子。
任何人都能看到我所缺少的东西? 编辑 - 请注意,这是在WordPress中。
function is_subpage() {
global $post; // load details about this page
if ( is_page() && $post->post_parent ) { // test to see if the page has a parent
return true; // return true, confirming there is a parent
} else { // there is no parent so ...
return false; // ... the answer to the question is false
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
由于加载所有子页面只是为了做一个简单的检查似乎有点资源密集,我今天早上想到了这个:
function has_children($p = null) {
global $post, $wpdb;
$pid = ($p) ? $p : $post->post_parent;
if ($pid) :
$r = $wpdb->get_row("SELECT ID FROM $wpdb->posts WHERE post_parent = $pid LIMIT 1");
if ($r) : return true; endif;
else :
return false;
endif;
}
您可以像使用(has_children())一样使用它,或者将其传递给后父项if(has_children($ post-post_parent)) - 如果你使用get_posts / get_pages而不是全局$ post,后者会很有用变量