WordPress 显示评论者等级并标注友链邻居和博主身份

这段代码可以根据评论者的评论数显示等级或 VIP 样式,并能标注显示博主的回复,这样的做法以来可以增加用户将互动,毕竟有些网友是很有虚荣心的,如果能显示自己的等级,会吸引更多的回访和提高等级。同时如果是友情链接中的 url,则会显示为邻居,具体代码,可以根据需要自行修改:

  1. // 获取访客VIP样式
  2. function get_author_class($comment_author_email, $comment_author_url)
  3. {
  4. global $wpdb;
  5. $adminEmail = get_bloginfo('admin_email');
  6. $styleClass = 'your css style'; //添加css样式
  7. $author_count = count($wpdb->get_results(
  8. "SELECT comment_ID as author_count FROM $wpdb->comments WHERE comment_author_email = '$comment_author_email' "
  9. ));
  10. if ($comment_author_email == $adminEmail) {
  11. echo '<a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="' . $styleClass . ' icon-vip vip7" title="博主"></a><a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  title="博主" class="icon-vip icon-admin"></a>';
  12. }
  13. $linkurls = $wpdb->get_results("SELECT link_url FROM $wpdb->links WHERE link_url = '$comment_author_url'");
  14. if ($author_count >= 1 && $author_count < 10 && $comment_author_email != $adminEmail) {
  15. echo '<a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="' . $styleClass . ' icon-vip vip1" title="评论达人 LV.1"></a>';
  16. } elseif ($author_count >= 10 && $author_count < 20 && $comment_author_email != $adminEmail) {
  17. echo '<a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="' . $styleClass . ' icon-vip vip2" title="评论达人 LV.2"></a>';
  18. } elseif ($author_count >= 20 && $author_count < 30 && $comment_author_email != $adminEmail) {
  19. echo '<a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="' . $styleClass . ' icon-vip vip3" title="评论达人 LV.3"></a>';
  20. } elseif ($author_count >= 30 && $author_count < 50 && $comment_author_email != $adminEmail) {
  21. echo '<a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="' . $styleClass . ' icon-vip vip4" title="评论达人 LV.4"></a>';
  22. } elseif ($author_count >= 50 && $author_count < 80 && $comment_author_email != $adminEmail) {
  23. echo '<a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="' . $styleClass . ' icon-vip vip5" title="评论达人 LV.5"></a>';
  24. } elseif ($author_count >= 80 && $author_coun < 200 && $comment_author_email != $adminEmail) {
  25. echo '<a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="' . $styleClass . ' icon-vip vip6" title="评论达人 LV.6"></a>';
  26. } elseif ($author_count >= 200 && $comment_author_email != $adminEmail) {
  27. echo '<a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="' . $styleClass . ' icon-vip vip7" title="评论达人 LV.7"></a>';
  28. }
  29. foreach ($linkurls as $linkurl) {
  30. if ($linkurl->link_url == $comment_author_url) {
  31. echo '<a class="vp" target="_blank" href="/links/" rel="external nofollow"  title="隔壁邻居的哦!" rel="noopener">隔壁邻居的哦!</a>';
  32. }
  33. }
  34. }

    这里将代码添加到当前主题 Functions.php 中。

    同时,我们需要将下面代码添加到”<?php comment_author_link();?>”评论模板下面。

    1. <?php get_author_class($comment->comment_author_email,$comment->comment_author_url,$comment->user_id)?>

    如果需要加上博主认证的,需要加上:

    1.  <?php get_author_class($comment->comment_author_email,$comment->user_id)?>
    2.  <?php if(user_can($comment->user_id, 1)){echo "<a title='博主认证' class='vip'></a>";}; ?>

    我们还需要添加样式:

    1. /**博客VIP评论样式**/
    2. .vp,.vip,.vip1,.vip2,.vip3,.vip4,.vip5,.vip6,.vip7{background: url(images/vip.png) no-repeat;display: inline-block;overflow: hidden;border: none;}
    3. .vp{background-position:-515px -3px;width: 14px;height: 14px;margin-bottom: -2px;}
    4. .vip{background-position:-495px -2px;width: 15px;height: 16px;margin-bottom: -3px;}
    5. .vip1{background-position:-1px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
    6. .vip2{background-position:-63px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
    7. .vip3{background-position:-144px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
    8. .vip4{background-position:-227px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
    9. .vip5{background-position:-331px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
    10. .vip6{background-position:-441px -2px;width: 46px;height: 14px;margin-bottom: -1px;}
    11. .vip7{background-position:-611px -2px;width: 46px;height: 14px;margin-bottom: -1px;}

    这里,我们需要添加图片,配合样式中调用的图片(images/vip.png)。

    
    
本站资源均来源于网络或网友投稿,部分资源未经测试,难免存在BUG,所有资源只限于学习研究,不得商用。如使用本站下载的资源造成任何损失或发生侵权行为,均与本站无关。如不接受本声明请勿下载!本站资源如有侵权,请联系QQ:497149677核实后立即删除!
最客资源网 » WordPress 显示评论者等级并标注友链邻居和博主身份