今天在网友威言威语博客上看到一个有趣的功能:评论者等级,根据评论者已发布的评论数量,划分不同的等级,并在昵称右边标记一个带等级数字的符号。
评论者等级功能,一方面可以给经常评论的博友一个身份标识,另一方面也能推动浏览者愿意参与互动,提升网站的用户粘度。
这么有趣的功能,我当然心痒痒了,马上动手给小站也添加上。
1、将以下核心代码添加到主题的 functions.php
文件中。
//评论者等级function get_comment_author_level($user_id, $comment_author_email) {if ($user_id && user_can($user_id, 'manage_options')) {return '<span class="post-author">博主</span>';}$email = sanitize_email($comment_author_email);if (empty($email)) return '';$comment_count = get_cached_comment_count($email);$levels = [499 => ['level' => 12, 'name' => '神话'],369 => ['level' => 11, 'name' => '传奇'],269 => ['level' => 10, 'name' => '无双'],189 => ['level' => 9, 'name' => '泰斗'],129 => ['level' => 8, 'name' => '宗师'],89 => ['level' => 7, 'name' => '大侠'],59 => ['level' => 6, 'name' => '豪侠'],39 => ['level' => 5, 'name' => '侠客'],24 => ['level' => 4, 'name' => '游侠'],13 => ['level' => 3, 'name' => '少侠'],6 => ['level' => 2, 'name' => '新锐'],2 => ['level' => 1, 'name' => '新秀'],];foreach ($levels as $threshold => $info) {if ($comment_count >= $threshold) {return sprintf('<span class="com-level vip%d" title="%s"><strong>V</strong><sub>%d</sub></span>',$info['level'],esc_attr($info['name']),$info['level']);}}return '';}// 获取评论数(分组缓存)function get_cached_comment_count($email) {global $wpdb;$email = sanitize_email($email);if (empty($email)) return 0;// 根据首字母分组(a-z 0-9),其余归 other$first = strtolower($email[0]);$group = ctype_alnum($first) ? $first : 'other';$cache_key = 'comment_counts_group_' . $group;// 获取该组缓存$comment_counts = get_transient($cache_key);if (!is_array($comment_counts)) $comment_counts = [];if (!isset($comment_counts[$email])) {// 数据库查询$count = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(comment_ID)FROM $wpdb->commentsWHERE comment_approved = 1AND comment_author_email = %sAND (comment_type = '' OR comment_type = 'comment')",));$comment_counts[$email] = $count;set_transient($cache_key, $comment_counts, 2 * HOUR_IN_SECONDS);}return $comment_counts[$email];}// 清理某邮箱的缓存function clear_comment_count_cache($comment_id) {$comment = get_comment($comment_id);if (!$comment || empty($comment->comment_author_email)) return;$email = sanitize_email($comment->comment_author_email);if (empty($email)) return;$first = strtolower($email[0]);$group = ctype_alnum($first) ? $first : 'other';$cache_key = 'comment_counts_group_' . $group;$comment_counts = get_transient($cache_key);if ($comment_counts !== false && isset($comment_counts[$email])) {unset($comment_counts[$email]); // 移除该邮箱缓存set_transient($cache_key, $comment_counts, 2 * HOUR_IN_SECONDS);}// 顺带清理最新评论和归档页评论数统计的缓存delete_transient('sidebar_recent_comments');delete_transient('archives_comment_stats');}// 评论新增/编辑/删除/状态变更时清理缓存add_action('comment_post', function($comment_id, $comment_approved) {if ($comment_approved == 1) {clear_comment_count_cache($comment_id);}}, 10, 2);add_action('wp_set_comment_status', function($comment_id, $status) {clear_comment_count_cache($comment_id);}, 10, 2);add_action('edit_comment', function($comment_id) {clear_comment_count_cache($comment_id);}, 10, 1);add_action('delete_comment', function($comment_id) {clear_comment_count_cache($comment_id);}, 10, 1);
2、在主题 - 自定义 - 额外 CSS 中添加样式与配色,里面包含了等级标识和数字标记。
/* 评论者等级 */
.post-author{margin:0 5px 0 4px;cursor:default;color:#ffffff;background-color:#000000;}/* 博主 */
.com-level{margin:0 5px 0 4px;cursor:default;}
.com-level sub{margin-left:-1px;font-weight:bold;vertical-align:-1px;font-size:10px;font-family:sans-serif,Helvetica,Arial;}
.vip1{color:#c0c0c0;} /* 微光灰 */
.vip2{color:#a0a0a0;} /* 哑光灰 */
.vip3{color:#9e7a5d;} /* 古朴青铜 */
.vip4{color:#b87333;} /* 经典青铜 */
.vip5{color:#cb6d1e;} /* 赤铜 */
.vip6{color:#cc8400;} /* 暗金 */
.vip7{color:#d4af37;} /* 古典金 */
.vip8{color:#ffb800;} /* 亮金 */
.vip9{color:#ffa500;} /* 华贵金 */
.vip10{color:#ff8c00;} /* 流光金 */
.vip11{color:#da70d6;} /* 紫金 */
.vip12{color:#a42be2;} /* 至尊紫金 */
3、在 comments.php 或自定义的评论函数中,比如在评论者的昵称旁边添加调用代码、
<?php echo get_comment_author_level($comment->user_id, $comment->comment_author_email); ?>
作者给评论者等级设计了一个 江湖风格的升级体系:新秀 → 新锐 → 少侠 → 游侠 → 侠客 → 豪侠 → 大侠 → 宗师 → 泰斗 → 无双 → 传奇 → 神话,依照 资历 → 实力 → 声望,逐级递进。每个称呼都经过精心斟酌,体现了江湖气息的升级体验。这样评论者的昵称后面就会带上等级标识,整个评论区会更有“江湖气息”。
修改代码时,发现我使用的 Autumn-Pro 主题原本是有评论等级功能的,但作者在更新升级后废止了,他设定了 初来乍到 → 江湖少侠 → 崭露头角 → 自成一派 → 横扫千军 → 登峰造极 → 一统江湖 共7个等级,竟然也是江湖侠客的称呼。
威言威语网友还发布了一个 走心评论 Touching Comments 插件,看着也挺不错,有空时我再折腾下,让网站多些乐趣。
历史上的今天:
- 2022: 二十四节气:霜降 (0)
- 2022: 10月23日,世界雪豹日 (0)
- 2021: 纪念日大全 (1)
- 2021: 【KVP学堂】WPS 表格如何制作米字格书法纸 (0)
我来看看升级了没。哈哈