为 wordpress 文章作者在评论留言时显示“本文作者”提示

  • 时间:2020-05-17 12:16:18
  • 分类:网络文摘
  • 阅读:136 次

如果你的 wordpress 博客有很多作者,那么当作者在自己的文章中回复读者评论留言时,在名称后面显示“本文作者”提示,可以让读者明确知道是作者亲自回复自己的留言,这对促进作者与读者之间的互动会很有帮助。当然,对于单一作者的博客,还是用网上盛传的“管理员“提示更好些。

为 wordpress 文章作者在评论留言时显示“本文作者”提示

首先将下面判断文章作者代码添加到当前 wordpress主题函数模板 functions.php 中:

  1. / 判断文章作者
  2. function zm_comment_by_post_author( $comment = null ) {
  3.     if ( is_object( $comment ) && $comment->user_id > 0 ) {
  4.         $user = get_userdata( $comment->user_id );
  5.         $post = get_post( $comment->comment_post_ID );
  6.         if ( ! emptyempty( $user ) && ! emptyempty( $post ) ) {
  7.             return $comment->user_id === $post->post_author;
  8.         }
  9.     }
  10.     return false;
  11. }

将显示调用代码添加到主题评论模板显示评论者名称代码的后面即可。

  1. <?php
  2.     $post_author = zm_comment_by_post_author( $comment );
  3.     if ( $post_author ) {
  4.         echo '<span class="post-author">文章作者</span>';
  5.     }
  6. ?>

不同主题评论模板代码不同,具体加到哪个位置,只能自行研究了。

同时显示管理员和作者的调用方法:

  1. <?php
  2.     if ($comment->comment_author_email == get_option('admin_email')) {
  3.         echo '<span class="author-admin">博主</span>';
  4.     } else {
  5.         $post_author = zm_comment_by_post_author( $comment );
  6.         if ( $post_author ) {
  7.             echo '<span class="post-author">作者</span>';
  8.         }
  9.     }
  10. ?>

判断作者代码取自WordPress默认主题Twenty Twenty,默认主题虽然外观看似简单,但功能真的很强大,有很多东西值得挖掘。

原文:https://zmingcx.com/wordpress-comment-is-article-author.html

推荐阅读:
身体健康无虚证的人不宜食用枸杞子  食疗养生枸杞子泡水喝有4大保健功效  调味品酱油对人体健康会有什么影响  便秘患者需要注意 常吃香蕉不能通便  食疗养生:女性常吃哪些食物能保健养颜  食药总局要求加强白酒质量安全监管  食品认证乱象:有钱就给有机食品证书  食品安全法中应该设立有奖举报制度  哪些健脑食品有助于增强孩子的记忆力  食药监总局:明确乳粉企业监督检查规定 
评论列表
添加评论