
WordPress 标签页
我们的一位读者询问是否可以在 WordPress 中的每条评论旁边突出显示用户角色?显示用户角色标签会重视网站上注册用户(特别是作者、编辑和管理员)发表的评论。在本文中,我们将向您展示如何在 WordPress 中的评论旁边轻松添加用户角色标签。
我们的一位读者询问是否可以在 WordPress 中的每条评论旁边突出显示用户角色?显示用户角色标签可以让您网站上的注册用户(尤其是作者、编辑和管理员)发表的评论得到重视。在本文中,我们将向您展示如何在 WordPress 评论旁边轻松添加用户角色标签。

为什么在WordPress中的评论作者姓名旁边显示用户角色标签? (Why Show User Role Label Next to Comment Author Name in WordPress?)
If you 允许用户注册 on your website or run a 多作者 WordPress 网站, then user labels can introduce users to each other based on their 用户角色.
如果您允许用户在您的网站上登记或运行多作者 WordPress 网站 ,则用户标签可以根据用户角色将用户介绍给彼此。
For example, users with the 编辑者用户角色 will show a badge next to their name in comments letting other users know that this comment was made by an editor.
例如,具有编辑者用户角色的用户将在评论名称旁边显示一个徽章,以使其他用户知道此评论是由编辑者做出的。
它可以建立用户信任并提高用户对您网站评论的参与度。
它可以建立用户信任并提高用户对您网站上的评论的参与度。
Many WordPress themes only 突出显示帖子作者的评论. They don’t show labels for any other user roles even if other comments are made by registered users or site administrators.
许多WordPress主题仅突出显示帖子作者的评论 。 即使已注册用户或站点管理员发表了其他评论,它们也不会显示任何其他用户角色的标签。
话虽这么说,让我们看看如何在 WordPress 中的评论旁边轻松添加用户角色标签。
话虽如此,让我们看看如何在 WordPress 中的评论旁边轻松添加用户角色标签。
在WordPress中的评论作者姓名旁边添加用户角色标签 (Adding User Role Label Next to Comment Author Name in WordPress)
This tutorial requires you to add code to your WordPress theme files. If you haven’t done this before, then please take a look at our guide on how to easily 在 WordPress 中复制并粘贴代码.
本教程要求您将代码添加到WordPress主题文件中。 如果您以前没有做过,请查看我们的指南,了解如何轻松地在 WordPress 中复制并粘贴代码 。
First thing you need to do is add the following code to your theme’s 函数.php file or a 特定于站点的插件.
您需要做的第一件事是将以下代码添加到主题的函数.php文件或具体的于在网站的插件中 。
if ( ! class_exists( 'WPB_Comment_Author_Role_Label' ) ) :
class WPB_Comment_Author_Role_Label {
public function __construct() {
add_filter( 'get_comment_author', array( $this, 'wpb_get_comment_author_role' ), 10, 3 );
add_filter( 'get_comment_author_link', array( $this, 'wpb_comment_author_role' ) );
}
// Get comment author role
function wpb_get_comment_author_role($author, $comment_id, $comment) {
$authoremail = get_comment_author_email( $comment);
// Check if user is registered
if (email_exists($authoremail)) {
$commet_user_role = get_user_by( 'email', $authoremail );
$comment_user_role = $commet_user_role->roles[0];
// HTML output to add next to comment author name
$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;
}
// Display comment author
function wpb_comment_author_role($author) {
return $author .= $this->comment_user_role;
}
}
new WPB_Comment_Author_Role_Label;
endif;
This function code above 钩子 into WordPress filters used to display comment author name to include user role label.
上面的此功能代码与WordPress过滤器挂钩,该过滤器用于显示评论作者姓名以包括用户角色标签。
您现在可以访问任何带有评论的帖子以查看其实际效果。注册用户发表的评论将在评论作者姓名旁边显示其用户角色。非注册用户发表的任何评论只会显示评论作者姓名。
现在您可以访问任何带有评论的帖子以查看其实际效果。注册用户发布的评论将在评论作者姓名旁边显示其用户角色。非注册用户发表的任何评论只会显示评论作者的姓名。

现在我们已经添加了用户角色,是时候对其进行样式设置并使其看起来干净了。
现在我们已经添加了用户角色,是时候对其进行样式设置并使其看起来干净了。
在我们的代码中,我们为每个用户角色添加了一个 CSS 类,因此我们可以使用这些 CSS 类以不同的方式自定义每个用户徽章(即使用不同的颜色等)
在我们的代码中,我们为每个用户角色添加了一个 CSS 类,因此我们可以使用这些 CSS 类以不同的方式自定义每个用户徽章(即使用不同的颜色等)
您可以使用以下示例 CSS 作为起点:
您可以使用以下示例 CSS 作为起点:
.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;
}
您可以根据自己的喜好随意调整 CSS。这是我们的演示网站上的样子:
您可以根据自己的喜好随意调整 CSS。这是我们的演示网站上的样子:

We hope this article helped you learn how to add user role label next to comments in WordPress. You may also want to see our guide on 如何在 WordPress 评论中延迟加载头像.
我们希望本文能帮助您学习如何在WordPress注释旁边添加用户角色标签。 您可能还想查看有关如何在 WordPress 评论中延迟加载头像指南。
If you liked this article, then please subscribe to our YouTube 频道 for WordPress video tutorials. You can also find us on 推特 and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube 频道 WordPress视频教程。 您也可以在推特和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-user-role-label-next-to-comments-in-wordpress/
WordPress 标签页