您可以使用get_ancestors()
检查帖子是否有祖父母id或祖父母id等。。
但是你必须检查每一个帖子。。。
function rt_before_after($content) {
if(is_page() || is_single()) {
global $post;
$ancestors = get_ancestors( $post->ID, \'page\' );
if ( in_array( 27, $ancestors ) || $post->ID == \'27\') {
$ntest=\'<div class="clearlogo"><img alt="nodal Clear logo" width="150" src="\'.get_stylesheet_directory_uri().\'/images/nodal-clear-logo.png"></div>\';
}else{
$ntest=\'\';
}
$beforecontent = $ntest.\'<h1 class="title">\'.get_the_title().\'</h1>\';
$fullcontent = $beforecontent . $content;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter(\'the_content\', \'rt_before_after\');
从父母到孩子,你可以用另一种方式
get_pages()
使用这种方法,您可以将其移动到单独的函数并调用一次,如果需要,甚至可以缓存它。我会选择第二种方法。
function rt_before_after($content) {
if(is_page() || is_single()) {
global $post;
// get all the child pages and grandchild etc..
$pages = get_pages( [\'child_of\'=>27] );
$pages_ids = array_map( function( $item ) {
return $item->ID;
}, $pages );
if ( in_array( $post->ID, $pages_ids ) || $post->ID == \'27\') {
$ntest=\'<div class="clearlogo"><img alt="nodal Clear logo" width="150" src="\'.get_stylesheet_directory_uri().\'/images/nodal-clear-logo.png"></div>\';
}else{
$ntest=\'\';
}
$beforecontent = $ntest.\'<h1 class="title">\'.get_the_title().\'</h1>\';
$fullcontent = $beforecontent . $content;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter(\'the_content\', \'rt_before_after\');