通过本文的方法,可以在WordPress每条评论显示注册用户角色标签。文章源自知更鸟-https://zmingcx.com/show-wordpress-comments-user-role.html
代码一,自动在评论者名称后面添加文章源自知更鸟-https://zmingcx.com/show-wordpress-comments-user-role.html
代码添加到当前主题函数模板 functions.php 中:文章源自知更鸟-https://zmingcx.com/show-wordpress-comments-user-role.html
if ( ! class_exists( 'ZM_Comment_Author_Role_Label' ) ) : class ZM_Comment_Author_Role_Label { public function __construct() { add_filter( 'get_comment_author', array( $this, 'zm_get_comment_author_role' ), 10, 3 ); add_filter( 'get_comment_author_link', array( $this, 'zm_comment_author_role' ) ); } // 获取评论角色 function zm_get_comment_author_role( $author, $comment_id, $comment ) { $authoremail = get_comment_author_email( $comment); // 检查用户是否注册 if (email_exists($authoremail)) { $commet_user_role = get_user_by( 'email', $authoremail ); $comment_user_role = $commet_user_role->roles[0]; // 输出HTML $this->comment_user_role = '<span class="comment-author-label comment-author-label-' . $comment_user_role . '">' . ucfirst( $comment_user_role ) . '</span>'; } else { $this->comment_user_role = ''; } return $author; } // 显示评论角色 function zm_comment_author_role( $author ) { return $author .= $this->comment_user_role; } } new ZM_Comment_Author_Role_Label; endif;
之后,会在每条评论用户名称后面显示其注册角色。文章源自知更鸟-https://zmingcx.com/show-wordpress-comments-user-role.html
代码二、自定义显示位置文章源自知更鸟-https://zmingcx.com/show-wordpress-comments-user-role.html
代码添加到当前主题函数模板 functions.php 中:文章源自知更鸟-https://zmingcx.com/show-wordpress-comments-user-role.html
function zm_get_comment_author_role() { global $author, $comment_id, $comment; $authoremail = get_comment_author_email( $comment); // 检查用户是否注册 if (email_exists($authoremail)) { $commet_user_role = get_user_by( 'email', $authoremail ); $comment_user_role = $commet_user_role->roles[0]; // 输出HTML $comment_user_role = '<span class="comment-author-label comment-author-label-' . $comment_user_role . '">' . ucfirst( $comment_user_role ) . '</span>'; } else { $comment_user_role = ''; } echo $comment_user_role; }
在评论模板适当位置添加:文章源自知更鸟-https://zmingcx.com/show-wordpress-comments-user-role.html
<?php zm_get_comment_author_role(); ?>
最后,添加相应的CSS样式。文章源自知更鸟-https://zmingcx.com/show-wordpress-comments-user-role.html
.comment-author-label { padding: 5px; font-size: 14px; border-radius: 3px; } .comment-author-label-editor { background-color: #efefef; } .comment-author-label-author { background-color: #faeeee; } .comment-author-label-contributor { background-color: #f0faee; } .comment-author-label-subscriber { background-color: #eef5fa; } .comment-author-label-administrator { background-color: #fde9ff; }
文章源自知更鸟-https://zmingcx.com/show-wordpress-comments-user-role.html
另一段更简单的代码:文章源自知更鸟-https://zmingcx.com/show-wordpress-comments-user-role.html
<?php $user_id = get_comment( get_comment_ID() )->user_id; if ( $user_id ){ $user_info = get_userdata( $user_id ); echo implode( ', ', $user_info->roles ) ; } ?>
直接添加到评论模板适当位置。文章源自知更鸟-https://zmingcx.com/show-wordpress-comments-user-role.html

2022年5月22日 21点43分 1F
2022年5月22日 21点45分 2F
大写的牛逼,原来还可以这样
2022年5月23日 10点14分 3F
这个可以用起来,很适用,权限分明