WordPress 添加评论等级 WP Comments Vip霓虹建站 > 后花园 > WordPress

看到不少朋友都添加了评论之星,也就是按照评论的数量,设定评论等级。

方法一:代码法

1.在主题的 functions.php 添加下面的代码:

/**
 * WordPress 添加评论之星
 * http://www.nhstu.com/15038.html
 */
function get_author_class($comment_author_email,$user_id){
    global $wpdb;
    $author_count = count($wpdb->get_results(
    "SELECT comment_ID as author_count FROM $wpdb->comments WHERE comment_author_email = '$comment_author_email' "));
    /如果不需要管理员显示VIP标签,就把下面一行的 // 去掉/
    // $adminEmail = get_option('admin_email');if($comment_author_email ==$adminEmail) return;
    if($author_count&gt;=10 <span style="color:#219">&amp;&amp; $author_count<20)</span>
        echo '<span style="color:#FF00FF"><a</span> <span style="color:#00c">class</span>=<span style="color:#a11">"vip1"</span> <span style="color:#00c">title</span>=<span style="color:#a11">"评论达人 LV.1"</span><span style="color:#FF00FF">></span><span style="color:#FF00FF"></a</span><span style="color:#FF00FF">></span>';
    else if($author_count&gt;=20 <span style="color:#219">&amp;&amp; $author_count<40)</span>
        echo '<span style="color:#FF00FF"><a</span> <span style="color:#00c">class</span>=<span style="color:#a11">"vip2"</span> <span style="color:#00c">title</span>=<span style="color:#a11">"评论达人 LV.2"</span><span style="color:#FF00FF">></span><span style="color:#FF00FF"></a</span><span style="color:#FF00FF">></span>';
    else if($author_count&gt;=40 <span style="color:#219">&amp;&amp; $author_count<80)</span>
        echo '<span style="color:#FF00FF"><a</span> <span style="color:#00c">class</span>=<span style="color:#a11">"vip3"</span> <span style="color:#00c">title</span>=<span style="color:#a11">"评论达人 LV.3"</span><span style="color:#FF00FF">></span><span style="color:#FF00FF"></a</span><span style="color:#FF00FF">></span>';
    else if($author_count&gt;=80 <span style="color:#219">&amp;&amp; $author_count<160)</span>
        echo '<span style="color:#FF00FF"><a</span> <span style="color:#00c">class</span>=<span style="color:#a11">"vip4"</span> <span style="color:#00c">title</span>=<span style="color:#a11">"评论达人 LV.4"</span><span style="color:#FF00FF">></span><span style="color:#FF00FF"></a</span><span style="color:#FF00FF">></span>';
    else if($author_count&gt;=160 <span style="color:#219">&amp;&amp; $author_count<320)</span>
        echo '<span style="color:#FF00FF"><a</span> <span style="color:#00c">class</span>=<span style="color:#a11">"vip5"</span> <span style="color:#00c">title</span>=<span style="color:#a11">"评论达人 LV.5"</span><span style="color:#FF00FF">></span><span style="color:#FF00FF"></a</span><span style="color:#FF00FF">></span>';
    else if($author_count&gt;=320 <span style="color:#219">&amp;&amp; $author_count<640)</span>
        echo '<span style="color:#FF00FF"><a</span> <span style="color:#00c">class</span>=<span style="color:#a11">"vip6"</span> <span style="color:#00c">title</span>=<span style="color:#a11">"评论达人 LV.6"</span><span style="color:#FF00FF">></span><span style="color:#FF00FF"></a</span><span style="color:#FF00FF">></span>';
    else if($author_count>=640)
        echo '<span style="color:#FF00FF"><a</span> <span style="color:#00c">class</span>=<span style="color:#a11">"vip7"</span> <span style="color:#00c">title</span>=<span style="color:#a11">"评论达人 LV.7"</span><span style="color:#FF00FF">></span><span style="color:#FF00FF"></a</span><span style="color:#FF00FF">></span>';
}

2.打开主题的评论文件(一般在根目录的 comments.php 或 functions.php 里),找到:

<span style="color:#f00"><?php</span> <span style="color:#@cm-word">comment_author_link</span>() <span style="color:#f00">?></span>
在后面添加:
<span style="color:#f00"><?php</span> <span style="color:#@cm-word">get_author_class</span>(<span style="color:#000-2">$comment</span><span style="color:#000">-&gt;</span><span style="color:#@cm-word">comment_author_email</span>,<span style="color:#000-2">$comment</span><span style="color:#000">-></span><span style="color:#@cm-word">user_id</span>); <span style="color:#f00">?></span><span style="color:#f00"><?php</span> <span style="color:#00f">if</span>(<span style="color:#@cm-word">user_can</span>(<span style="color:#000-2">$comment</span><span style="color:#000">-></span><span style="color:#@cm-word">user_id</span>, <span style="color:#164">1</span>)){<span style="color:#00f">echo</span> <span style="color:#a11">"<a title='站长' class='vip'></a>"</span>;}; <span style="color:#f00">?></span>
3.下载下面的图片,添加到主题的 images文件夹 4.在主题的 style.css 文件的最后,添加下面的样式代码:
<span style="color:#a50">/VIP评论之星/</span>
.<span style="color:#FF00FF">vp</span>,.<span style="color:#FF00FF">vip</span>,.<span style="color:#FF00FF">vip1</span>,.<span style="color:#FF00FF">vip2</span>,.<span style="color:#FF00FF">vip3</span>,.<span style="color:#FF00FF">vip4</span>,.<span style="color:#FF00FF">vip5</span>,.<span style="color:#FF00FF">vip6</span>,.<span style="color:#FF00FF">vip7</span>{<span style="color:#000">background</span>: <span style="color:#164">url</span><span style="color:#164">(images</span>/<span style="color:#164">vip</span>.<span style="color:#164">png</span><span style="color:#164">)</span> <span style="color:#164">no-repeat</span>;<span style="color:#000">display</span>: <span style="color:#164">inline-block</span>;<span style="color:#000">overflow</span>: <span style="color:#164">hidden</span>;<span style="color:#000">border</span>: <span style="color:#164">none</span>;}
.<span style="color:#FF00FF">vp</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-515px</span> <span style="color:#164">-2px</span>;<span style="color:#000">width</span>: <span style="color:#164">16px</span>;<span style="color:#000">height</span>: <span style="color:#164">16px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-3px</span>;}
.<span style="color:#FF00FF">vp</span>:<span style="color:#FF00FF">hover</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-515px</span> <span style="color:#164">-22px</span>;<span style="color:#000">width</span>: <span style="color:#164">16px</span>;<span style="color:#000">height</span>: <span style="color:#164">16px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-3px</span>;}
.<span style="color:#FF00FF">vip</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-494px</span> <span style="color:#164">-3px</span>;<span style="color:#000">width</span>: <span style="color:#164">16px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-2px</span>;}
.<span style="color:#FF00FF">vip</span>:<span style="color:#FF00FF">hover</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-494px</span> <span style="color:#164">-22px</span>;<span style="color:#000">width</span>: <span style="color:#164">16px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-2px</span>;}
.<span style="color:#FF00FF">vip1</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-1px</span> <span style="color:#164">-2px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip1</span>:<span style="color:#FF00FF">hover</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-1px</span> <span style="color:#164">-22px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip2</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-63px</span> <span style="color:#164">-2px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip2</span>:<span style="color:#FF00FF">hover</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-63px</span> <span style="color:#164">-22px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip3</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-144px</span> <span style="color:#164">-2px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip3</span>:<span style="color:#FF00FF">hover</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-144px</span> <span style="color:#164">-22px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip4</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-227px</span> <span style="color:#164">-2px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip4</span>:<span style="color:#FF00FF">hover</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-227px</span> <span style="color:#164">-22px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip5</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-331px</span> <span style="color:#164">-2px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip5</span>:<span style="color:#FF00FF">hover</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-331px</span> <span style="color:#164">-22px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip6</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-441px</span> <span style="color:#164">-2px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip6</span>:<span style="color:#FF00FF">hover</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-441px</span> <span style="color:#164">-22px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip7</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-611px</span> <span style="color:#164">-2px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}
.<span style="color:#FF00FF">vip7</span>:<span style="color:#FF00FF">hover</span>{<span style="color:#000">background-position</span>:<span style="color:#164">-611px</span> <span style="color:#164">-22px</span>;<span style="color:#000">width</span>: <span style="color:#164">46px</span>;<span style="color:#000">height</span>: <span style="color:#164">14px</span>;<span style="color:#000">margin-bottom</span>: <span style="color:#164">-1px</span>;}

到这里,应该就差不多了。

方法二:插件法

如果你不想折腾代码,可以下载安装 WP Comments Vip 插件,也是非常方便的

标签: wp comments vip, wordpress 评论等级

联系我们期待您的来信!我们会认真诚实的对待每一位客户,有来信必将得到我们的回复!

谢谢您能联系我们!
嘿,我来帮您!