文章目录核心代码部署评论数获取与缓存优化评论者等级设计等级命名与风格样式与配色模板调用最近我给博客增加了 评论者等级 功能,一方面是给经常评论的博友一个身份标识,另一方面也能让更多人愿意参与互动。
其实功能实现并不复杂:核心思路是 根据评论邮箱,统计该评论者已批准的评论数量,然后划分到不同的「等级」区间,最终输出一个带等级数字和提示文本的 HTML 标签。
核心代码部署将以下核心代码添加到主题的 functions.php 文件中即可实现基础功能:
//评论者等级function get_comment_author_level($user_id, $comment_author_email) { if ($user_id && user_can($user_id, 'manage_options')) { return ' span 博主 /span $email = sanitize_email($comment_author_email); if (empty($email)) return ''; $comment_count = get_cached_comment_count($email); $levels = [['level' = 12, 'name' = '神话'],['level' = 11, 'name' = '传奇'],['level' = 10, 'name' = '无双'],['level' = 9, 'name' = '泰斗'],['level' = 8, 'name' = '宗师'],['level' = 7, 'name' = '大侠'],['level' = 6, 'name' = '豪侠'],['level' = 5, 'name' = '侠客'],['level' = 4, 'name' = '游侠'],['level' = 3, 'name' = '少侠'],['level' = 2, 'name' = '新锐'],['level' = 1, 'name' = '新秀'],foreach ($levels as $threshold = $info) { if ($comment_count = $threshold) { $current_level_name = $info['name']; $current_level_num = $info['level']; $next_levels = array_keys($levels); $next_threshold = null; foreach ($next_levels as $idx = $key) { if ($key == $threshold && isset($next_levels[$idx - 1])) { $next_threshold = $next_levels[$idx - 1]; break; if ($next_threshold) { $next_level_name = $levels[$next_threshold]['name']; $need = $next_threshold - $comment_count; $title = sprintf( '%s · %d评 · 再%d评升至%s', $current_level_name, $comment_count, $need, $next_level_name } else { $title = sprintf( '%s · %d评 · 已达最高级', $current_level_name, $comment_count return sprintf( ' span strong V /strong sub %d /sub /span ', $current_level_num, esc_attr($title), $current_level_num 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- comments WHERE1 AND%s AND (comment_type = '' OR)", $email $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);// 评论新增/编辑/删除/状态变更时清理缓存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);评论数获取与缓存优化评论数的统计如果每次都查数据库,会带来额外负担。为此我做了一个 分组缓存机制:
按邮箱首字母分组,把同一组的邮箱统计结果缓存在一个 transient 里;这样避免了「每个邮箱单独缓存」或「所有邮箱一起缓存」的极端情况;对于 评论量超过一万条 的博客,这种分组缓存能显著减少数据库查询压力;等级的title中新增进度提示,成长目标一目了然。缓存设置为 2小时自动清理。此外,我还在评论新增、编辑、删除、状态变更时,触发清理对应分组缓存,保证数据一致性。
如果你不在乎实时性,其实这一步也可以省略,因为评论等级的变化频率通常不高。去掉上面代码中 // 清理某邮箱的缓存 后面的代码就行。
评论者等级设计我设计了 12个等级。12 是一个相对合适的数字:既能涵盖大部分评论者,又能为未来留出拓展空间。
在划分等级区间时,我根据博客上 1.7万多条博友评论数据进行统计分析:
绝大多数人的评论数量不多,所以前期的等级区间要小一些;后期等级则逐渐拉大,避免升级过于频繁;只有一条评论的用户不展示标识,所以区间从 2 开始;同时预留 2-3 个高阶区间,给未来活跃评论者使用。经过多次尝试,ChatGPT提供了一个基于对数分析的分组方案,我在此基础上进行了微调,得出了最终的分组数据。
等级命名与风格每个人都可以根据喜好来命名等级,我个人喜欢武侠,所以设计了一个 江湖风格的升级体系:
新秀 → 新锐 → 少侠 → 游侠 → 侠客 → 豪侠 → 大侠 → 宗师 → 泰斗 → 无双 → 传奇 → 神话
依照 资历 → 实力 → 声望,逐级递进。
每个称呼都经过我精心斟酌,体现了江湖气息的升级体验。
样式与配色在前端样式上,我定义了 .com-level 类,里面包含了等级标识和数字标记。示例代码如下:
/* 评论者等级 */.post-author{font-size:12px;margin-left:4px;padding:1px 4px 2px 4px;background:#555;border-radius:4px;color:#fff;}.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;} /* 至尊紫金 */颜色的思路是 逐级递增,从低阶的金属质感(黑铁、青铜、赤铜),逐步过渡到高阶的金色与紫金。
上面的代码用 V 字符代替图标,实际应用中你可以换成合适的图标。
将所有等级的图标放在一起看更有感觉一些。
123456789101112
最后,在 comments.php 或自定义的评论函数中,比如在评论者的昵称旁边添加:
?php echo get_comment_author_level($comment- user_id, $comment- comment_author_email); ?这样评论者的昵称后面就会带上等级标识,整个评论区会更有“江湖气息”。