我目前有一个功能,可以检查页面是否是父级,如果是,则执行一些操作。但我还想检查父页面是否有包含特定slug的子页面。
基本上,我应该如何检查WordPress页面是否有与my-child-slug
?
简而言之,这是我当前的功能:
/* Using Slug, make iconbox Shortcode */
function fs_sc_location_iconbox( $atts ){
// begin output buffering
ob_start();
global $post; // if outside the loop
$response = \'Nothing to see here\';
if ( $post->post_parent ) {
// This is a subpage
$slug = $post->post_name;
} else {
// This is not a subpage
$slug = $post->post_name;
// Check if has child page with slug of my-child-slug
// ** NEED LOGIC HERE **
// if this parent page has a child with the desired child slug...
$response = \'Yes, this parent has a child page that we are looking for.\';
}
// if there is a slug
if ($slug) {
// output the default response, unless this page is a parent of the desired child page
echo $response;
}
// end output buffering, grab the buffer contents, and empty the buffer
return ob_get_clean();
}
add_shortcode( \'fs_location_iconbox\', \'fs_sc_location_iconbox\' );
如何更新te
// ** NEED LOGIC HERE **
区域,以便检查此父页是否有
my-child-slug
鼻涕虫如果是,那么它将为
$response
变量
最合适的回答,由SO网友:Garconis 整理而成
现在,我可以通过在我的函数中添加以下内容来实现它:
$slug_to_check = \'my-child-slug\';
// go through each child page we found
foreach( $child_pages as $child_page ) {
// get the slug of this child page we found
$child_slug = $child_page->post_name;
// get the link of this child page we found
$child_link = get_page_link( $child_page->ID );
// if the child page we found has the slug we want
if($child_slug === $slug_to_check) {
// output the child page link
$response = \'Yes, this parent has a child page that we are looking for.\';
}
}