如何实现WordPress读者墙按评论等级显示相应读者?
我们国内很多 WordPress 主题都会带有一个读者墙功能,Nana 主题也不一例外,但是看多了按总评论数的排行榜,是否有点腻歪?而且我们大多数主题都有评论等级的功能,是否想过读者按评论等级逐级显示出来?今天就教大家如何实现 WordPress 读者墙按评论等级显示相应读者。大概的效果图如下:
1、制作评论榜单模板。以 Nana 主题为例,找到 template-readers 文件,复制一份并重名为 template-pinglunbang,然后打开该文件,找到以下代码
Template Name: 读者百强榜
修改为
Template Name: 评论榜单
继续找到以下代码
- <?php
- function readers_wall($limit = “100”)
- {
- global $wpdb;
- $counts = $wpdb->get_results(“SELECT count(comment_author) AS cnt, comment_author, comment_author_url, comment_author_email FROM $wpdb->comments WHERE user_id!=’1′ AND comment_author!=’1′ AND comment_approved=’1′ AND comment_type=” GROUP BY comment_author ORDER BY cnt DESC LIMIT $limit”);
- $i = 0;
- $type=”;
- foreach ($counts as $count ) {
- $i++;
- $c_url = $count->comment_author_url;
- if (!$c_url) {
- $c_url = “https://boke112.com”;
- }
- $tt = $i;
- if ($i == 1) {
- $tt = “读者之青龙”;
- }
- else if ($i == 2) {
- $tt = “读者之白虎”;
- }
- else if ($i == 3) {
- $tt = “读者之朱雀”;
- }
- else if ($i == 4) {
- $tt = “读者之玄武”;
- }
- else {
- $tt = “第” . $i . “名”;
- }
- $avatar = my_avatar( $count->comment_author_email,36,$default=”,$count->comment_author);
- if ($i < 5) {
- $type .= “<a . $i . “” target=”_blank” href=”” . $c_url . “” title=”【” . $tt . “】评论:” . $count->cnt . “”><h4>【” . $tt . “】</h4>”.$avatar.“<strong>” . $count->comment_author . “</strong>” . $c_url . “</a>”;
- }
- else {
- $type .= “<a target=”_blank” href=”” . $c_url . “” title=”【” . $tt . “】评论:” . $count->cnt . “”>” . $avatar . $count->comment_author . “</a>”;
- }
- }
- echo $type;
- }
- ?>
- <div class=“readers”>
- <?php readers_wall(100);?>
- >
</div
修改为
- <?php
- function readers_wall($limit = 100)
- {
- global $wpdb;
- $counts = $wpdb->get_results(“SELECT count(comment_author_email) AS cnt, comment_author, comment_author_url, comment_author_email FROM $wpdb->comments WHERE user_id!=’1′ AND comment_approved=’1′ AND comment_type=” AND comment_author_url!=” GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 0 , $limit”);
- $i1 = 0;$i2 = 0;$i3 = 0;$i4 = 0;$i5 = 0;$i6 = 0;$i7 = 0;$i8 = 0;
- $dj = 0;
- $xiucai = ”;
- $juren = ”;
- $jinshi = ”;
- $zhuangyuan = ”;
- $shangshu = ”;
- $chengxiang = ”;
- $wangye = ”;
- $huangdi = ”;
- foreach ($counts as $count ) {
- $dj=$count->cnt;
- $c_url = $count->comment_author_url;
- $avatar = my_avatar( $count->comment_author_email,36,$default=”,$count->comment_author);
- if($dj >= 20 && $dj < 40){
- $i1++;
- $xiucai .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【秀才榜】第” . $i1 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
- }
- if($dj >= 40 && $dj < 80){
- $i2++;
- $juren .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【举人榜】第” . $i2 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
- }
- if($dj >= 80 && $dj < 160){
- $i3++;
- $jinshi .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【进士榜】第” . $i3 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
- }
- if($dj >= 160 && $dj < 320){
- $i4++;
- $zhuangyuan .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【状元榜】第” . $i4 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
- }
- if($dj >= 320 && $dj < 640){
- $i5++;
- $shangshu .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【尚书榜】第” . $i5 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
- }
- if($dj >= 640 && $dj < 1280){
- $i6++;
- $chengxiang .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【丞相榜】第” . $i6 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
- }
- if($dj >= 1280 && $dj < 2560){
- $i7++;
- $wangye .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【王爷榜】第” . $i7 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
- }
- if($dj >= 2560 && $dj < 100000){
- $i8++;
- $huangdi .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【皇帝榜】第” . $i8 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
- }
- }
- if(!empty($xiucai)){$xiucai =“<h6 ><strong>读者评论等级排行榜(秀才榜)</strong></h6><div >”.$xiucai.“>”;}
- if(!empty($juren)){$juren =“<h6 ><strong>读者评论等级排行榜(举人榜)</strong></h6><div >”.$juren.“>”;}
- if(!empty($jinshi)){$jinshi =“<h6 ><strong>读者评论等级排行榜(进士榜)</strong></h6><div >”.$jinshi.“>”;}
- if(!empty($zhuangyuan)){$zhuangyuan =“<h6 ><strong>读者评论等级排行榜(状元榜)</strong></h6><div >”.$zhuangyuan.“>”;}
- if(!empty($shangshu)){$shangshu =“<h6 ><strong>读者评论等级排行榜(尚书榜)</strong></h6><div >”.$shangshu.“>”;}
- if(!empty($chengxiang)){$chengxiang =“<h6 ><strong>读者评论等级排行榜(丞相榜)</strong></h6><div >”.$chengxiang.“>”;}
- if(!empty($wangye)){$wangye =“<h6 ><strong>读者评论等级排行榜(王爷榜)</strong></h6><div >”.$wangye.“>”;}
- if(!empty($huangdi)){$huangdi =“<h6 ><strong>读者评论等级排行榜(皇帝榜)</strong></h6><div >”.$huangdi.“>”;}
- echo $huangdi;echo $wangye;echo $chengxiang;echo $shangshu;echo $zhuangyuan;echo $jinshi;echo $juren;echo $xiucai;
- }
- readers_wall(120);?>
</div
保存并更新文件。
PS:以上代码中的评论的等级名称及相对应的评论数量请自行修改即可。</div
2、新建评论榜单页面。后台新建一个页面,模板就选我们刚才新创建的“评论榜单”然后直接修好好固定链接之后直接发布即可。
你可能感兴趣的文章
- 优化WordPress评论楼层号,可同时支持顺序倒序排列
- 纯代码给WordPress侧边栏小工具添加幻灯片功能
- 为WordPress主题导航菜单添加个性图标字体
- 出现Failed AddprinterrDriver2错误解决办法
- 南宁江南大道科目三电子考详细步骤
- 广西广电网络机顶盒和宽带网上如何缴费?
- 如何实现zblogPHP站点自动添加图片alt属性
- 给WordPress文章图片添加Fancybox 3灯箱效果教程
</p