我想添加一个图像到父页面及其所有子页面,包括第二级孙子。我不想在任何其他页面上显示该图像。
以下是我目前掌握的情况:
if(is_page() || is_single()) {
global $post;
if ($post->post_parent == \'27\' || $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=\'\';
}
这适用于第一级和父母,但不适用于孙子。我曾想过添加第二级页面ID,但认为有更好的方法。我觉得我错过了一些简单的事情。
我在一个函数中编写这篇文章,该函数将把它添加到带有\\u内容的内容中(下面是完整内容):
function rt_before_after($content) {
if(is_page() || is_single()) {
global $post;
if ($post->post_parent == \'27\' || $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\');