首页 论坛 wordpress判断用户等级来显示不同的评论头像
帖子详情

wordpress程序的评论头像是自动的调用全球gravatar头像,wordpress程序本身是不支持用户设置头像的,网站后台只支持“对于那些没有自定义头像的用户,您可以显示一个通用头像或者根据他们的邮件地址产生一个头像。”网站管理员可以设置一些“小怪物头像”。

wordpress判断用户等级来显示不同的评论头像

如果你自己要设置自己头像,必须使用你的邮箱到gravatar网站上去设置。随着gravatar网站被国内屏蔽,即使你设置了个性头像,也没法在自己网站上显示出来。

为了解决这个问题,有以下二个方法:

方法一:安装WordPress 自定义头像插件:WP User Avatar

方法二:根据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的字样)
    <?php
    if (is_admin_comment($comment->comment_ID)){?>
    <img src="<?php bloginfo(‘template_directory’); ?>/images/1.jpg" width="48px" alt="管理员头像"/>
    <?php } elseif (is_editor_comment($comment->comment_ID)){?>
    <img src="<?php bloginfo(‘template_directory’); ?>/images/2.jpg" width="48px" alt="编辑头像"/>
    <?php } elseif (is_author_comment($comment->comment_ID)){?>
    <img src="<?php bloginfo(‘template_directory’); ?>/images/3.jpg" width="48px" alt="作者头像"/>
    <?php } elseif (is_Contributor_comment($comment->comment_ID)){?>
    <img src="<?php bloginfo(‘template_directory’); ?>/images/4.jpg" width="48px" alt="投稿者头像"/>
    <?php } elseif (is_subscriber_comment($comment->comment_ID)){?>
    <img src="<?php bloginfo(‘template_directory’); ?>/images/5.jpg" width="48px" alt="订阅者头像"/>
    <?php } else {?>
    <img src="<?php bloginfo(‘template_directory’); ?>/images/6.jpg" width="48px" alt="游客头像"/>
    <?php }?>
  6. 这样当有用户在网站上发布评论时,网站程序就会自动判断用户的级别,并且显示设定的相应的头像了。
    wordpress判断用户等级来显示不同的评论头像

如果只想分为管理员和普通用户二种用户类别的头像,可以这样写:

<?php if (is_admin_comment($comment->comment_ID)){?>
<img alt="学做网站论坛讲师" src="https://www.52diyhome.com/wp-content/themes/xwz/images/zqy.jpg" class="avatar avatar-32 photo" height="32" width="32">
<?php }else{?>
<img alt="学习建网站学员" src="https://www.52diyhome.com/wp-content/uploads/2022/10/xy.gif" class="avatar avatar-32 photo" height="32" width="32">
<?php }?>

版权:言论仅代表个人观点,不代表官方立场。转载请注明出处:https://www.52diyhome.com/forum/65185.html

发表评论
暂无评论
  • 0 +

    访问总数

  • 0 +

    会员总数

  • 0 +

    资源总数

  • 0 +

    今日发布

  • 0 +

    本周发布

  • 0 +

    运行天数

资源在于分享,创作来源想象