Notice: Trying to access array offset on value of type bool in /www/wwwroot/weimahe.com/wp-content/advanced-cache.php on line 472

Notice: Trying to access array offset on value of type bool in /www/wwwroot/weimahe.com/wp-content/advanced-cache.php on line 472
WordPress 评论显示用户角色 - 微码盒
!
也想出现在这里? 联系我们
广告位

WordPress 评论显示用户角色

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

代码

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

  1. if ( ! class_exists( 'ZM_Co| # A * ) % X omment_Author_Role_Label'K n x 3 Q q 6 y ) ) :
  2. class ZM_Comment_Author_Role_Label {
  3. public function __constru. 6 \ S ] C Z =ct() {
  4. add_? [ 4 U cfilter( 'get_comment_author', array( $this, 'zm_get_comment_author_role' ), 10, 3 );
  5. add_filter( 'get_commen~ h \ & ht_author_link', array( $this, 'zm_comment_author_role' ) );
  6. }
  7. // 获取评论角色
  8. functs B # = $ w = h {ion zm_get_comment_author_role( $aut! D yhor, $c9 & D G iomment_id,e t 9 5 t $comment ) {x C [ %;
  9. $authoremail = get_comment_author_email( $comment);
  10. // 检R * / . c查用户是否注册
  11. if (emaib l 4 h o Q ll_exists($authoremail)) {
  12. $commet_user_role = get_user_by( 'email', $authoremail^ ] r ^ D );
  13. $comment_user_role = $commet_user_role->roles[0];
  14. //V & 4 5 e , F 输出HTML
  15. $this->comment_user_role = '<span class=&r X T k ? N k \ bquot;comment-author-label comment-author-labe! f \ \ + z P el-' . $comment_user_role . '">' . ucfirst( $comment_user_role ) . '</span>v ? ]';q s & v q
  16. } else {
  17. $this->comment_user_role = '';
  18. }
  19. return $author;
  20. }
  21. // 显示评论角色
  22. fun4 : { L * # \ Nction z% W u D F B Gm_i ~ N , l ] !comment_author_ro6 W L C ! A w - -le( $a} 5 T Z I i Q f [uthor ) {
  23. return $autho# _ i ]r .= $this->comment_user_role;
  24. }
  25. }
  26. new ZM_Comment_Author_Role_Label;
  27. endif;

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

代码二

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

  1. function zm_get_z K n Rcomment_author_role() {
  2. global $aj ) Q [ - = \ !uthor, $comment_id, $commentz M ! f 6 l e U [;
  3. $authoremail = get_comment_author_email( $comment);
  4. // 检查用户是否注册
  5. if (email_exists($authoremail. ; H R ? O ; H041;) {
  6. $commet_user_role = get_user_by( 'email', $authoremail );
  7. $comr 2 h N , 6 5 ]ment_user_role = $commet_user_role->roles[0];
  8. // 输出HTML
  9. $comment_user_role = '<spanT X 9 r - ` class="comment-author-label coG \ { \ I ~ c 8mmenx s ^ f ) ^t-author-label-' . $comment_user_role . '&r N pquot;>' . ucfirstR ; ] @ | M U040; $comment_user_role ) . '</span>';
  10. } else {
  11. $comment_user_role = '';
  12. }
  13. echo $comment_user_role;
  14. }

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

  1. <?php zm_3 % 7 aget_comment_author_role(); ?>

最后g f & @ r H F,添加相应的 CSS 样式。

  1. .comment-author-label {
  2. padding: 5px;
  3. font-size: 14px;
  4. border-radius: 3px;
  5. }
  6. .commO & ~ )ent-author-label-editor {
  7. background-color: #efefef;
  8. v ] J125;
  9. .comment-author-label-autP U 7 _ thor {
  10. background-color: #faeeee;
  11. }
  12. .comment-author-label-contribue s F z Y ) e , [tor {
  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. 4 2 R g y 0 L -5;

另一段更简单的代码:

  1. <?php
  2. $useA 4 ? Kr_id = get_comment( get_comment_ID() )->user_id;
  3. if ( $user_id ){
  4. $uw 3 m k ? r : 4 vser_info = get_userdata( $user_id );
  5. echo implo, - U \ J Z [ : ede( ', ', $user_info->roles s { L z1; ;
  6. }
  7. ?>

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

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

WordPress 菜单添加自定义选项打造超级菜单

2022-5-23 13:55:43

WordPress教程

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

2022-5-24 1:56:04

下载说明

  • 1、微码盒所提供的压缩包若无特别说明,解压密码均为weimahe.com
  • 2、下载后文件若为压缩包格式,请安装7Z软件或者其它压缩软件进行解压;
  • 3、文件比较大的时候,建议使用下载工具进行下载,浏览器下载有时候会自动中断,导致下载错误;
  • 4、资源可能会由于内容问题被和谐,导致下载链接不可用,遇到此问题,请到文章页面进行反馈,以便微码盒及时进行更新;
  • 5、其他下载问题请自行搜索教程,这里不一一讲解。

站长声明

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有;若为付费资源,请在下载后24小时之内自觉删除;若作商业用途,请到原网站购买;由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索