给WordPress评论者添加等级显示,在一定程度上能够刺激用户发言的积极性,今天钱柜娱乐官网就分享下目前在用的评论样式以及实现代码!
作为一个博客型站点,评论功能起着举足轻重的作用,可以说访客的一言一语让整个站点“活”起来了。这时候适当给评论者加个评论等级,更有种锦上添花的感觉。一方面可以很直观的看到哪些是忠实的读者,另一外面也在一定程度上刺激了读者发言的兴趣。
有朋友留言咨询,今天钱柜娱乐官网就分享下目前在用的评论样式以及实现代码!
上图所示是目前钱柜娱乐官网在用的评论等级样式(2018.4),其中包含评论者等级显示、站长认证、友情链接认证。其原理是查询数据库根据评论者邮箱已评论条数显示不同的内容;友链认证是根据读者填入的链接地址判断是否为友情链接而输出内容;站长认证是判断读者填入的邮箱是否为站长输出内容,为了防止有人冒充站长进行留言,建议开启《WordPress禁止冒充管理员评论》功能。
至于输出什么内容,本站使用的是直接输出字体图标,您可酌情修改为文字或图片等;
将如下代码放入主题函数模板中(functions.php)
//评论VIP标志 function get_author_class($comment_author_email,$comment_author_url){ global $wpdb; $adminEmail = '此处写入站长邮箱'; $author_count = count($wpdb->get_results( "SELECT comment_ID as author_count FROM $wpdb->comments WHERE comment_author_email = '$comment_author_email' ")); if($comment_author_email ==$adminEmail) echo '<a class="vp" target="_blank" href="http://www.iyaxi.com/about.html" title="经鉴定,这货是站长"><i class="wi wi-crown"></i><span class="lv">站长</span></a>'; $linkurls = $wpdb->get_results( "SELECT link_url FROM $wpdb->links WHERE link_url = '$comment_author_url'"); foreach ($linkurls as $linkurl) { if ($linkurl->link_url == $comment_author_url ) echo '<a class="vip" target="_blank" href="http://www.iyaxi.com/links.html" title="合作商或友情链接认证"><i class="wi wi-heart"></i></a>'; } if($author_count>=1 && $author_count<10 && $comment_author_email!=$adminEmail) echo '<a class="vip1" target="_blank" href="http://www.iyaxi.com/about.html" title="评论之星 LV.1"><i class="wi wi-level-1"></i></a>'; else if($author_count>=10 && $author_count<20 && $comment_author_email!=$adminEmail) echo '<a class="vip2" target="_blank" href="http://www.iyaxi.com/about.html" title="评论之星 LV.2"><i class="wi wi-level-2"></i></a>'; else if($author_count>=20 && $author_count<50 && $comment_author_email!=$adminEmail) echo '<a class="vip3" target="_blank" href="http://www.iyaxi.com/about.html" title="评论之星 LV.3"><i class="wi wi-level-3"></i></a>'; else if($author_count>=50 && $author_count<100 && $comment_author_email!=$adminEmail) echo '<a class="vip4" target="_blank" href="http://www.iyaxi.com/about.html" title="评论之星 LV.4"><i class="wi wi-level-4"></i>/a>'; else if($author_count>=100 && $author_count<200 && $comment_author_email!=$adminEmail) echo '<a class="vip5" target="_blank" href="http://www.iyaxi.com/about.html" title="评论之星 LV.5"><i class="wi wi-level-5"></i></a>'; else if($author_count>=200 && $author_count<500 && $comment_author_email!=$adminEmail) echo '<a class="vip6" target="_blank" href="http://www.iyaxi.com/about.html" title="评论之星 LV.6"><i class="wi wi-level-6"></i></a>'; else if($author_count>=500 && $comment_author_email!=$adminEmail) echo '<a class="vip7" target="_blank" href="http://www.iyaxi.com/about.html" title="评论之星 LV.7"><i class="wi wi-level-7"></i></a>'; }
将下面函数放入到评论列表模板里,一般放在评论者昵称后面
<?php get_author_class($comment->comment_author_email,$comment->comment_author_url)?>
样式代码就不贴出来了,大家可根据自己的实际情况,进行修改以及样式调整。
刷新页面,是不是已经实现了呢?
楼下说的那个友链不错,下次弄上
@憧憬点滴记忆: 来这里多学习~!
我呢我呢 怎么看等级
不给CSS是逼我们自己扒吗![[大兵]](https://www.yaxi.net/wp-content/themes/wing/img/smilies/db.gif)
能够显示友链标注的方法很不错
@懿古今: 有个问题就是用户填写的地址跟友链里的地址完全相同才可以,少个斜杠都不行
@飞鸟: 这样可以通过判断两个地址的域名,去掉斜杠和http或https
加入了无效果![[难过]](https://www.yaxi.net/wp-content/themes/wing/img/smilies/ng.gif)
不错哈,有时间也去增加一个友链评论的标志,是博客的友链就应该享受更高的待遇![[偷笑]](https://www.yaxi.net/wp-content/themes/wing/img/smilies/tx.gif)
看来我的等级更低
@银色月航: 等级就像体重,多吃几顿、多评几次,自然会上来的
@飞鸟: 加上等级啦
我就想看看我是什么等级~
@西枫里博客: 你是大佬等级