Wordpress php:测试页面是否是父页面的子项的方法? [英] Wordpress php: Way to test if pages are children of a parent page?
问题描述
在我的第一个Wordpress网站上工作,所以我确信这是一个非常基本的问题。我正在努力编写一个条件的php语句,当页面是父页面的子页面时执行某个操作。
Working on my first Wordpress site so I'm sure this is a really basic question. I'm struggling to write a conditional php statement that performs a certain action when a page is a child of a parent page.
例如,而不是只指定一个页面,如下所示,我想指定所有将关于我们页面作为父母的页面:
For example, rather than just specifying one page, as below, I'd like to specify all pages that have the 'About Us' page as a parent:
<?php if (is_page('About Us')) echo 'Hello World!'; ?>
我尝试了child_of功能,但并不像我希望的那样简单。
I've tried the "child_of" function but it wasn't as straightforward as I'd hoped.
当我使用下面的内容时,我收到语法错误 - 可能只是我不知道如何使用该函数:
When I use the below, I get a syntax error - probably just me not knowing how to use the function:
<?php if (child_of('About Us')) echo 'Hello World!'; ?>
有什么建议吗?
推荐答案
您收到错误,因为WordPress中没有 child_of()
函数。
You're getting an error because there is no such thing as a child_of()
function in WordPress.
child_of()
是一种使用get_pages()函数进行搜索的方式。
child_of()
is a way of searching using the get_pages() function.
$pages = get_pages('child_of=##');
其中##是关于我们页面的数字ID(不是名称)。
where ## is the numeric ID (not the name) of the 'About us' page.
这篇关于Wordpress php:测试页面是否是父页面的子项的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!