我现在正在构建一个wordpress主题,我在侧边栏中使用条件来显示每个页面的不同信息,如下所示:
if (is_page('services')) {
etc.....
} elseif (etc....
但是,有些子页面没有自己的特定条件。我怎样才能使父页面的条件也适用于它的子页面?
谢谢!
答案 0 :(得分:1)
你可以通过多种方式做到这一点。可能有一种更简单的方法,但这就是我要做的: 在页面本身上,检查它是否是子页面:
if ( is_page() && $post->post_parent )
{
// This is a subpage
return true;
}
else
{
// This is not a subpage
return false;
}
将检查页面是否为子项,在这种情况下,您可以使用与当前相同的方式从父项中获取条件,除非在检查时指定父项而不是当前页。
答案 1 :(得分:0)
您也可以将这段代码放在functions.php文件中,并在wp代码的任何位置使用它。
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
$parentID = $post->post_parent; // the ID of the parent is this
return $parentID; // return the ID
} else { // there is no parent so...
return false; // ...the answer to the question is false
};
};