wordpress程序的评论头像是自动的调用全球gravatar头像,wordpress程序本身是不支持用户设置头像的,网站后台只支持“对于那些没有自定义头像的用户,您可以显示一个通用头像或者根据他们的邮件地址产生一个头像。”网站管理员可以设置一些“小怪物头像”。
如果你自己要设置自己头像,必须使用你的邮箱到gravatar网站上去设置。随着gravatar网站被国内屏蔽,即使你设置了个性头像,也没法在自己网站上显示出来。
为了解决这个问题,有些人安装了某些头像的wordpress插件来辅助实现设置功能,而今天分享给大家的这个功能是根据wordpress程序开发手册写出的通过判断用户的等级去显示他的头像。
1.wordpress网站把用户分为5个等级,分别为:管理员、编辑、作者、投稿者、订阅者,当然不要忘记给没有注册的游客也做一张头像图片。我们首先需要对这6个用户的用户各自创建一个头像图片,图片的大小为48px*48px。
2.将这5张图片分别取名为1.jpg,2.jpg,3.jpg,4.jpg,5.jpg ,6.jpg,并把它们放到网站主题文件夹下的images文件夹。
代码部署
3.将以下函数放到自己网站的模板函数functions.php中;
- //评论 判断管理员
- function is_admin_comment( $comment_ID = 0 ) {
- $comment = get_comment( $comment_ID );
- $admin_comment = false;
- if($comment->user_id == 1){
- $admin_comment = true;
- }
- return $admin_comment;
- }
- //评论 判断编辑
- function is_editor_comment( $comment_ID = 0 ) {
- $comment = get_comment( $comment_ID );
- $editor_comment = false;
- if($comment->user_id == 1){
- $editor_comment = true;
- }
- return $editor_comment;
- }
- //评论 判断作者
- function is_author_comment( $comment_ID = 0 ) {
- $comment = get_comment( $comment_ID );
- $author_comment = false;
- if($comment->user_id == 1){
- $author_comment = true;
- }
- return $author_comment;
- }
- //评论 判断投稿者
- function is_Contributor_comment( $comment_ID = 0 ) {
- $comment = get_comment( $comment_ID );
- $Contributor_comment = false;
- if($comment->user_id == 1){
- $Contributor_comment = true;
- }
- return $Contributor_comment;
- }
- //评论 判断订阅者
- function is_subscriber_comment( $comment_ID = 0 ) {
- $comment = get_comment( $comment_ID );
- $subscriber_comment = false;
- if($comment->user_id == 1){
- $subscriber_comment = true;
- }
- return $subscriber_comment;
- }
4.以上函数是用来判断网站文章的评论者是哪一个等级。
5.将以下的代码放到自己网站的评论模板comments.php中,查找一下评论模板中显示头像的代码。(含有gravatar的字样)
6.这样当有用户在网站上发布评论时,网站程序就会自动判断用户的级别,并且显示设定的相应的头像了。
若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如果喜欢,请打赏支持本站,谢谢大家!
2020年4月27日 16:16 沙发
看看
2020年12月7日 00:27 板凳
路过学习
2021年10月22日 13:47 地板
哎呀我去,还必须留言才能看!
2022年8月22日 18:43 4楼