是否可以检查页面是父页面还是子页面?
我的页面设置如下:
--父级
-子页1
-子页2
等。
如果是父页面,我想显示某个菜单;如果它在子页面,我想显示一个不同的菜单。
我知道我可以像下面这样做,但我想让它变得更有动态性,而不包括特定的页面ID。
<?php
if ($post->post_parent == '100') { // if current page is child of page with page ID 100
// show image X
}
?>发布于 2012-12-17 23:23:44
您可以测试该帖子是否是一个子页面,如下所示:
*(来自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
}
?>发布于 2015-07-14 01:17:01
我知道这是一个古老的问题,但我一直在寻找同样的问题,直到我想出了这个问题,才找到一个明确而简单的答案。我的答案没有回答他的解释,但它回答了主要问题,也就是我正在寻找的东西。
这将检查页面是子页面还是父页面,并允许您仅在既是子页面又是父页面的页面上显示侧边栏菜单,而不是在既没有父页面也没有子页面的页面上显示。
<?php
global $post;
$children = get_pages( array( 'child_of' => $post->ID ) );
if ( is_page() && ($post->post_parent || count( $children ) > 0 )) :
?>发布于 2014-11-21 00:18:09
将此函数放入主题的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
}
?>https://stackoverflow.com/questions/13916783
复制相似问题