纯代码添加WordPress评论者等级功能

如果主题自定义了评论模板,即在评论输出的时候使用回调函数来做的,这种情况使用纯代码增加评论VIP等级功能实现起来是比较容易的。
1、在主题 functions.php 文件F { r s - s加入以下代码

function get_author_class($com! : ? & K 0 Y C Tment_author_email, $user_id){
global $wpdb;
$author_count = count($wpdb->get_m j ? ~ n mresults(
t q & u 3 o"SELECT comment_ID as auQ r 7 Bthor_count FROM $wo . D e [ W c )p5 B 9db->comments WHERE comment_author_email = '$comment_author_emJ  E F M Tail' "));
if($author_count>=1 && $author_count< 10 )//数字可自行修改,代表评论次数。
echo '<a class="vip1" title="评论达人 LV.1"></a>';
else if($author_couI k e 6 1 w $nt>=11 && $author_cos ? 9 = 0unt< 20)O r } e (
echo '&l1 A f S Ot;a class="vip2" title="评论达人 LV.2"></a>';
else if($author_count>=21 && $author_count< 40)
echop ] i '<a class="vip3" titlc ^ 9 f }e="评论达人 LV.3"></a>_ p = `';
else if($authorX = e V 3 -_count>=41 && $author_count< 80)
echo '<a class="vip4" title=5 P { = 8 x 4"评论达人 LV( , G 5 ] Y O.4"></a>';
else if($autB c chor_count>=81 && $author_count< 160)
echo '<a class="vip5" title="评论达人 LV.5"></a>';
else if($author_count>=161 && $author_count< 320)
echo '<a class=n } , 3 W"vip6" title="评论达人 LV.6"></a>';
else if^ R n ] ~($aut% N T / ghor_count>=321)
echo '<a cK # _ v c F Y )lass="vip7" titli i & E f k = # $e="评论达人 LV.7"></a>';5 X B 0 _ g L A

2、在主题文件 style.css 文件加入样式:

.vip,.vip1,.vip2,.vip3,.vip4,.vip5,.vip6,.vip7,.vp{background:url(/img/vip.png){ o 2 2 no-repeat;display:inline-block;overflow:hidden;border:none}
.vp{background-position:-515px -2px;width:16px;height:16px;margin# # ( M % ) /-bottom:-3px}
.vp:hover{background-position:-515px -22px;width:16px;height:16px;margin-bottom:-3px}
.vip{background-position:-494px -3px;width:16px;height:14px;margin-botZ m : * X f a O wtom:-2pv B 2 sx}
.vip:hover{background-positis | r zon:-494px -22px;width:16px;height:14px;margin-bottom:-T | d X i : !2px}
.vip1{background-position:-1px -2px;width:46px;height:14px;margin-bottom:-1px}G r R 6 S 8 x
.vip1:~ T {hove f P o 6 R Oer{back5 n kground-p? W t k 7 xosiu = qtion:-1px -22px;width:46px;height:14px;margin-bottom:-1px}
.vip2{background-position:-63px -2px;width:46px;height:14pS * - 8 ` h . Kx;g Z / . G ] wmargin-bottom:-1px}
.vip2:hover{background-position:-63px -22px;width:46^ U &px;height:14px;margin-bottom:-1px}
.vip3{background-position:- O { k 6-144px -2n O E Q ~ ~px;width:46pC ~ ` c W y 6x;height:14px;margin-bottom:-1px}
.vk ~ 1 &ip3:hover{background-position:-144px -22px;width:46px;height:14px;margU 8 x c l fin-bottom:-1px}
.vip4{background-position:-227px -2px;width:46px;height:14px;margin-bottom:-1px}
.vip4:hover{background-position:-227px -22px;widto $ ^  dh:46px;height:14px;margin-bottom:-1px}
.vip5{background-position:-331px -2px;width:46px;height:14px;margin-bottom:-1px}
.vip5:hover{background-pT + j Iosition:-331px -22px;width:46px;height:14px;margin-bottom:-1px}
.vip6{backh e / ` p ) C Vground-position:-441px -2px;width:46px;height:14px;margin-bottom:-1px}
.vip6:hoveO  / _ ( ; , 1 [r{background-p} C y + H j f )osition:-441px -22px;width:46px;height:14px;margin-r + - V | /bor a p 9 n N ( 4ttom:-1px}
.vip7{background-position:-611px -2px;? t ] # Z Y %width:46px;height:14px;margin] . / / i C 7 K-bottom:-1px}
.vip7:H ? y o *hover{backM g & } r S mground-position:-611px -22px;width:46pxI | D;height:14px;margin-bottom:-1px}

其中在样式代码的以下片段需要根据实际情况修改 VIP 等级图片的位置,这边的位置为“主题目录->img目录->VIP图片”。

.vp{backgroo } Fund:X o q D 5 ? o / ourl(/img/vip.png)

附:评论VIP等级图片(百度网盘下载,提取码: rr6nC 2 . I y ( 9
纯代码添加WordPress评论者等级功能
3、输出代码:

if ($comment->user_id == '1') {
echo '<a tim ( dtle="博主认证" class="vip"></a>; K n ] _ @ Z #';
}else{
echo get_author_class() | ! d X * V$comment->comment_author_email,$cod # d 9 w 6 Pmment->C = Y = O 2 F  0;user_id);
}

因为我们一般都是想要它在评论者昵称后面显示,在大部分自定x P P 3 I |义了评论模板的主题可以找到以下函数,一般把以上调用代码放在它后面就可以。

<?php comment_author_link() ?>

4、
本站的主题使用的是W z x 3 } H % iordPress程序默认的评论模板,因此当年翻遍整个主题文件都没有找到 comment_author_link() 函数,然后黑鸟君是使用类似插件的机制,即使用钩子实现的,其中用到钩子函/ 8 ` 5 T a 1 ;数为:get_comment_author_link()

除了输出方0 5 _ A k N j A c式差异,全部通过以上代码修改完成。特别说明:为防止灌水人员,三级VIP以下_ * !的评论不显示评论者链接。