是否有可能检查一个页面是父母,或者如果它是一个子页?
我有我的网页设置是这样的:
- 家长
----儿童页1
----儿童页2
等等
我想表现出一定的菜单,如果它是一个父页面和不同的菜单,如果它的子页面上。
我知道我可以做类似下面,但我想让它更加动态有点不包括特定页面的ID。
<?php
if ($post->post_parent == '100') { // if current page is child of page with page ID 100
// show image X
}
?>
您可以测试后是这样子页面:
*(从http://codex.wordpress.org/Conditional_Tags )*
<?php
global $post; // if outside the loop
if ( is_page() && $post->post_parent ) {
// This is a subpage
} else {
// This is not a subpage
}
?>
将这个功能在你的主题的functions.php文件。
function is_page_child($pid) {// $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
$anc = get_post_ancestors( $post->ID );
foreach($anc as $ancestor) {
if(is_page() && $ancestor == $pid) {
return true;
}
}
if(is_page()&&(is_page($pid)))
return true; // we're at the page or at a sub page
else
return false; // we're elsewhere
};
然后你可以使用它:
<?php
if(is_page_child(100)) {
// show image X
}
?>
I know this is an old question but I was searching for this same question and couldn't find a clear and simple answer until I came up with this one. My answer doesn't answer his explanation but it answers the main question which is what I was looking for.
This checks whether a page is a child or a parent and allows you to show, for example a sidebar menu, only on pages that are either a child or a parent and not on pages that do not have a parent nor children.
<?php
global $post;
$children = get_pages( array( 'child_of' => $post->ID ) );
if ( is_page() && ($post->post_parent || count( $children ) > 0 )) :
?>