【问题标题】:Only Show text on Child Post wordpress仅在 Child Post wordpress 上显示文本
【发布时间】:2017-11-10 19:41:41
【问题描述】:

我想在我的 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');

如果我在父页面上使用此代码/帖子内容不显示。请帮帮我,我希望你明白我想说什么。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    试试这个:

    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);
    

    【讨论】:

    • 感谢您的帮助和您的时间,但同样的错误所有父页面/帖子内容不显示。
    • 尝试增加过滤钩子的优先级
    • 请您解释一下,以便我理解。
    • 官方链接developer.wordpress.org/reference/functions/add_filter为你添加_filter('the_content','wpdev_before_after', 99999);
    • 再次感谢,正如您提到的那样,我尝试了这个,但是当我在代码中使用 {} 时,父帖子的内容没有显示。显示帖子标题和其他内容,但未显示帖子内容。 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'); 在 if ($post->post_parent) 之后使用并在 return $fullcontent 之前关闭; .正如您在我的问题第二个代码中看到的那样,所以我尝试了您的代码但同样的错误。
    【解决方案2】:

    感谢@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);
    

    【讨论】:

    • 也许你应该接受@Tarun Mahashwari 给出的答案而不是你自己的答案。
    • 是的,但@Tarun Mahashwari 代码可能只适用于页面,所以我用这两个代码发布我的答案,我要求 Tarun Mahashwari 更新答案,当答案更新时我会标记,所以将来如果有任何用户需要,这将有所帮助。
    猜你喜欢
    • 2016-01-23
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    相关资源
    最近更新 更多