WordPress主题文章作者评论区留言高亮显示

 4533

WordPress文章作者在自己的文章中回复读者评论留言时,在名称后面显示“文本作者”提示,可以让读者明确知道是作者亲自回复自己的留言是不是有点小鸡冻呢。

适合多作者的博客网站,单一作者的博客,还是用网上盛传的“管理员“提示更好些。

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

  1. /**
  2.  * 检查指定的评论是否由评论文章的作者撰写。
  3.  *
  4.  */
  5. function twentytwenty_is_comment_by_post_author( $comment = null ) {
  6.     if ( is_object( $comment ) && $comment->user_id > 0 ) {
  7.         $user = get_userdata( $comment->user_id );
  8.         $post = get_post( $comment->comment_post_ID );
  9.         if ( ! empty( $user ) && ! empty( $post ) ) {
  10.  
  11.             return $comment->user_id === $post->post_author;
  12.         }
  13.     }
  14.     return false;
  15. }

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

  1. <?php
  2.     $post_author = twentytwenty_is_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 = twentytwenty_is_comment_by_post_author( $comment );
  6.         if ( $post_author ) {
  7.             echo '<span class="post-author">作者</span>';
  8.         }
  9.     }
  10. ?>

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



本文网址:https://www.zztuku.com/detail-7982.html
站长图库 - WordPress主题文章作者评论区留言高亮显示
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐