Php 仅在子帖子wordpress上显示文本

Php 仅在子帖子wordpress上显示文本,php,wordpress,Php,Wordpress,我想在我的wordpress附件页面上显示谷歌广告,这是要在wordpress上发布的子页面。我只想显示此广告的唯一子帖子/页面,而不是在父帖子上。我想在内容的末尾显示,所以我尝试了以下代码 function wpdev_before_after($content) { global $post; if ($post->post_parent) $aftercontent = 'Ad code here'; $fullcontent =$content . $af

我想在我的wordpress附件页面上显示谷歌广告,这是要在wordpress上发布的子页面。我只想显示此广告的唯一子帖子/页面,而不是在父帖子上。我想在内容的末尾显示,所以我尝试了以下代码

function wpdev_before_after($content) { 
  global $post;
if ($post->post_parent)  
    $aftercontent = 'Ad code here';
    $fullcontent =$content . $aftercontent;

    return $fullcontent;
 }
add_filter('the_content','wpdev_before_after');
上面的代码在父页面和子页面上都显示了广告,我也用这种方式尝试了这段代码

function wpdev_before_after($content) { 
  global $post;
if ($post->post_parent)  {
     $aftercontent = 'code here';
    $fullcontent =$content . $aftercontent;
}
    return $fullcontent;
 }
add_filter('the_content','wpdev_before_after');
如果我在父页面/帖子内容上使用此代码,则不会显示。请帮帮我,我希望你能理解我想说的话。

试试这个:

function wpdev_before_after($content) { 
  global $post;
if(is_singular() && $post->post_parent > 0)  {
     $aftercontent = 'code here';
     $fullcontent =$content . $aftercontent;
     return $fullcontent;
}

else
    return $content;
 }
add_filter('the_content','wpdev_before_after', 9999);

感谢@Tarun Mahashwari帮助我获得工作代码。 下面是帖子和子帖子的工作代码

function wpdev_before_after($content) { global $post; if ($post->post_parent) { $aftercontent = 'code here'; $fullcontent =$content . $aftercontent; return $fullcontent; } else return $content; } add_filter('the_content','wpdev_before_after');
此代码是否适用于@Tarun Mahashwari提供的页面和子页面

 function wpdev_before_after($content) { 
      global $post;
    if(is_page() && $post->post_parent > 0)  {
         $aftercontent = 'code here';
         $fullcontent =$content . $aftercontent;
         return $fullcontent;
    }

else
    return $content;
 }
add_filter('the_content','wpdev_before_after', 9999);

感谢您的帮助和您的时间,但其相同的错误是所有父页面/帖子内容均不显示。请尝试提高过滤器的优先级。请您解释一下,以便我能理解。您添加过滤器的官方链接('the_content','wpdev_before_after',9999);再次感谢,正如你提到的,我尝试了这个,但是当我在代码中使用{}时,没有显示父帖子的内容。文章的标题和其他东西都显示出来了,但文章的内容并没有显示出来<代码>函数wpdev_before_after($content){global$post;if($post->post_parent){$aftercontent='code here';$fullcontent=$content.$aftercontent;}返回$fullcontent;}添加_过滤器('the_content','wpdev u before_after')在if($post->post_parent)之后使用,并在返回$fullcontent;之前关闭。正如你在我的第二个问题代码中所看到的,所以我尝试了你的代码,但出现了相同的错误。也许你应该接受@Tarun Mahashwari给出的答案,而不是你自己的答案。当然可以,但是@Tarun Mahashwari代码可能只适用于页面,所以我用这两个代码发布了我的答案,我向泰伦·马哈什瓦里询问更新答案,当答案更新时,我会标记,所以将来如果有任何用户需要,这将有所帮助。