wordpress标签页面_如何在WordPress中的注释旁边添加用户角色标签

wordpress标签页面

One of our readers asked if it was possible to highlight user role next to each comment in WordPress? Displaying user role label gives weight to comments made by registered users on your website specifically authors, editors, and admins. In this article, we will show you how to easily add user role label next to comments in WordPress.

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

Add user role next to comments in WordPress
为什么在WordPress中的评论作者姓名旁边显示用户角色标签? (Why Show User Role Label Next to Comment Author Name in WordPress?)

If you allow user registration on your website or run a multi-author WordPress website, then user labels can introduce users to each other based on their user roles.

如果您允许用户在您的网站上注册或运行多作者WordPress网站 ,则用户标签可以根据用户角色将用户介绍给彼此。

For example, users with the editor user role will show a badge next to their name in comments letting other users know that this comment was made by an editor.

例如,具有编辑者用户角色用户将在评论名称旁边显示一个徽章,以使其他用户知道此评论是由编辑者做出的。

It builds up user trust and increases user engagement in comments on your website.

它建立了用户信任,并增加了用户对您网站上评论的参与度。

Many WordPress themes only highlight comments made by post author. They don’t show labels for any other user roles even if other comments are made by registered users or site administrators.

许多WordPress主题仅强调帖子作者的评论 。 即使已注册用户或站点管理员发表了其他评论,它们也不会显示任何其他用户角色的标签。

That being said, let’s take a look at how to easily add user role label next to comments in 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 copy and paste code in WordPress.

本教程要求您将代码添加到WordPress主题文件中。 如果您以前没有做过,请查看我们的指南,了解如何轻松地在WordPress中复制和粘贴代码

First thing you need to do is add the following code to your theme’s functions.php file or a site-specific plugin.

您需要做的第一件事是将以下代码添加到主题的functions.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 hooks into WordPress filters used to display comment author name to include user role label.

上面的此功能代码与WordPress过滤器挂钩,该过滤器用于显示评论作者姓名以包括用户角色标签。

You can now visit any post with comments to see it in action. Comments made by registered users will display their user role next to the comment author name. Any comment made by non-registered users will only display comment author name.

现在,您可以访问带有评论的任何帖子,以查看其运行情况。 注册用户发表的评论将在评论作者名称旁边显示其用户角色。 非注册用户发表的任何评论将仅显示评论作者姓名。

User role label shown next to their comment

Now that we have added the user role, it’s time to style it and make it look clean.

现在,我们已经添加了用户角色,现在该为它设置样式并使其看起来干净了。

In our code, we have added a CSS class for each user role, so we can use these CSS classes to customize each user badge differently (i.e use different colors, etc)

在我们的代码中,我们为每个用户角色添加了一个CSS类,因此我们可以使用这些CSS类以不同的方式自定义每个用户徽章(即使用不同的颜色,等等)

You can use the following sample CSS as an starting point:

您可以使用以下示例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;
}

Feel free to adjust the CSS to your liking. This is how it looked on our demo website:

可以根据自己的喜好随意调整CSS。 这是在我们的演示网站上的样子:

User role badges displayed with their comments

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 how to lazy load gravatars in WordPress comments.

我们希望本文能帮助您学习如何在WordPress注释旁边添加用户角色标签。 您可能还想查看有关如何在WordPress注释中延迟加载gravatars的指南。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-user-role-label-next-to-comments-in-wordpress/

wordpress标签页面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值