当前位置:首页文章笔记建站教程WordPress 评论显示用户角色

WordPress 评论显示用户角色

通过本文的方法,可以在 WordPress 每条评论显示注册用户角色标签。

代码一

自动在评论者名称后面添加代码添加到当前主题函数模板 functions.php 中:

  1. if ( ! class_exists( ‘ZM_Comment_Author_Role_Label’ ) ) :
  2. class ZM_Comment_Author_Role_Label {
  3. public function __construct() {
  4. add_filter( ‘get_comment_author’, array( $this, ‘zm_get_comment_author_role’ ), 10, 3 );
  5. add_filter( ‘get_comment_author_link’, array( $this, ‘zm_comment_author_role’ ) );
  6. }
  7. // 获取评论角色
  8. function zm_get_comment_author_role( $author, $comment_id, $comment ) {
  9. $authoremail = get_comment_author_email( $comment);
  10. // 检查用户是否注册
  11. if (email_exists($authoremail)) {
  12. $commet_user_role = get_user_by( ’email’, $authoremail );
  13. $comment_user_role = $commet_user_role->roles[0];
  14. // 输出HTML
  15. $this->comment_user_role = ‘<span class="comment-author-label comment-author-label-‘ . $comment_user_role . ‘">’ . ucfirst( $comment_user_role ) . ‘</span>’;
  16. } else {
  17. $this->comment_user_role = ;
  18. }
  19. return $author;
  20. }
  21. // 显示评论角色
  22. function zm_comment_author_role( $author ) {
  23. return $author .= $this->comment_user_role;
  24. }
  25. }
  26. new ZM_Comment_Author_Role_Label;
  27. endif;

之后,会在每条评论用户名称后面显示其注册角色。

代码二

自定义显示位置,代码添加到当前主题函数模板 functions.php 中

  1. function zm_get_comment_author_role() {
  2. global $author, $comment_id, $comment;
  3. $authoremail = get_comment_author_email( $comment);
  4. // 检查用户是否注册
  5. if (email_exists($authoremail)) {
  6. $commet_user_role = get_user_by( ’email’, $authoremail );
  7. $comment_user_role = $commet_user_role->roles[0];
  8. // 输出HTML
  9. $comment_user_role = ‘<span class="comment-author-label comment-author-label-‘ . $comment_user_role . ‘">’ . ucfirst( $comment_user_role ) . ‘</span>’;
  10. } else {
  11. $comment_user_role = ;
  12. }
  13. echo $comment_user_role;
  14. }

在评论模板适当位置添加:

  1. <?php zm_get_comment_author_role(); ?>

最后,添加相应的 CSS 样式。

  1. .comment-author-label {
  2. padding: 5px;
  3. font-size: 14px;
  4. border-radius: 3px;
  5. }
  6. .comment-author-label-editor {
  7. background-color: #efefef;
  8. }
  9. .comment-author-label-author {
  10. background-color: #faeeee;
  11. }
  12. .comment-author-label-contributor {
  13. background-color: #f0faee;
  14. }
  15. .comment-author-label-subscriber {
  16. background-color: #eef5fa;
  17. }
  18. .comment-author-label-administrator {
  19. background-color: #fde9ff;
  20. }

另一段更简单的代码:

  1. <?php
  2. $user_id = get_comment( get_comment_ID() )->user_id;
  3. if ( $user_id ){
  4. $user_info = get_userdata( $user_id );
  5. echo implode( ‘, ‘, $user_info->roles ) ;
  6. }
  7. ?>

直接添加到评论模板适当位置。

温馨提示:

文章标题:WordPress 评论显示用户角色

文章链接:https://www.wuyanshuo.cn/812.html

更新时间:2022年05月25日

本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:service@wuyanshuo.cn我们将第一时间处理! 资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。 所有资源仅限于参考和学习,版权归原作者所有,更多请阅读无言说网络服务协议

给TA打赏
共{{data.count}}人
人已打赏
建站教程

WooCommerce Rest API开发微信小程序和APP等第三方应用的鉴权方式

2022-5-25 17:10:00

建站教程

判断 WordPress 文章内容是否为空

2022-5-25 23:00:34

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
'