Hi!请登陆

纯代码实现按年、月、周评论排行的读者墙

2021-2-24 40 2/24

在前面已经介绍了张戈博主的《按年月周排行的读者墙插件》,但是有些博友想纯代码实现,但又不是很懂如何根据现有的读者墙修改成为按年、月、周评论排行的读者墙,今天就将本站使用的读者墙代码分享给大家,希望大家有空的时候折腾一下,从而推进按年、月、周评论排行的读者墙。

纯代码实现按年、月、周评论排行的读者墙

具体步骤如下:

1、把以下代码添加到我们的留言板模板文件的<?php the_content(); ?>下面,或get_template_part( 'content', 'page' );下面:

  1. <h3><?php _e('年度评论排行 TOP6','boke123'); ?></h3>
  2.     <?php
  3.         $author_email = get_option('admin_email');
  4.         $queryy="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE date_format(comment_date,'%Y')=date_format(now(),'%Y') AND user_id='0' AND comment_author_email != '" . $author_email . "' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 6";
  5.         $wally = $wpdb->get_results($queryy);
  6.         $maxNum = $wally[0]->cnt;
  7.         foreach ($wally as $commenty)
  8.             {
  9.                 $width = round(40/($maxNum / $commenty->cnt),2);
  10.                 if$commenty->comment_author_url )
  11.                     $urly = $commenty->comment_author_url;
  12.                 else $urly="#";
  13.                     $avatar = get_avatar( $commenty->comment_author_email, $size = '36',$default = get_bloginfo('template_directory') . '/avatar/'.'default.jpg');
  14.                     $tmpy = "<li><a target=\"_blank\" href=\"".$commenty->comment_author_url."\">".$avatar."<em>".$commenty->comment_author."</em> <strong>+".$commenty->cnt."</strong></br>".$commenty->comment_author_url."</a></li>";
  15.                     $outputy .= $tmpy;
  16.             }
  17.         $outputy = "<ul class=\"readers-list\">".$outputy."</ul>";
  18.         echo $outputy ;
  19.     ?>
  20. <h3><?php _e('本月评论排行 TOP6','boke123'); ?></h3>
  21.     <?php
  22.         $querym="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE date_format(comment_date,'%Y-%m')=date_format(now(),'%Y-%m') AND user_id='0' AND comment_author_email != '" . $author_email . "' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 6";
  23.         $wallm = $wpdb->get_results($querym);
  24.         $maxNum = $wallm[0]->cnt;
  25.         foreach ($wallm as $commentm)
  26.             {
  27.                 $width = round(40/($maxNum / $commentm->cnt),2);
  28.                 if$commentm->comment_author_url )
  29.                     $urlm = $commentm->comment_author_url;
  30.                 else $urlm="#";
  31.                     $avatar = get_avatar( $commentm->comment_author_email, $size = '36',$default = get_bloginfo('template_directory') . '/avatar/'.'default.jpg');
  32.                     $tmpm = "<li><a target=\"_blank\" href=\"".$commentm->comment_author_url."\">".$avatar."<em>".$commentm->comment_author."</em> <strong>+".$commentm->cnt."</strong></br>".$commentm->comment_author_url."</a></li>";
  33.                     $outputm .= $tmpm;
  34.             }
  35.         $outputm = "<ul class=\"readers-list\">".$outputm."</ul>";
  36.         echo $outputm ;
  37.     ?>
  38. <h3><?php _e('本周评论排行 TOP6','boke123'); ?></h3>
  39.     <?php
  40.         $queryw="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE yearweek(date_format(comment_date,'%Y-%m-%d')) = yearweek(now()) AND user_id='0' AND comment_author_email != '" . $author_email . "' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 6";
  41.         $wallw = $wpdb->get_results($queryw);
  42.         $maxNum = $wallw[0]->cnt;
  43.         foreach ($wallw as $commentw)
  44.             {
  45.                 $width = round(40/($maxNum / $commentw->cnt),2);
  46.                 if$commentw->comment_author_url )
  47.                     $urlw = $commentw->comment_author_url;
  48.                 else $urlw="#";
  49.                     $avatar = get_avatar( $commentw->comment_author_email, $size = '36',$default = get_bloginfo('template_directory') . '/avatar/'.'default.jpg');
  50.                     $tmpw = "<li><a target=\"_blank\" href=\"".$commentw->comment_author_url."\">".$avatar."<em>".$commentw->comment_author."</em> <strong>+".$commentw->cnt."</strong></br>".$commentw->comment_author_url."</a></li>";
  51.                     $outputw .= $tmpw;
  52.             }
  53.         $outputw = "<ul class=\"readers-list\">".$outputw."</ul>";
  54.         echo $outputw ;
  55.     ?>

其中LIMIT 6即为显示前6个读者;$default = get_bloginfo('template_directory') . '/avatar/'.'default.jpg')是默认头像地址,建议修改。

如果还没有留言板模板,可以复制所使用主题中的page.php文件,改名为page-zxly.php,然后新建一个页面,这个页面的地址改为zxly即可。

2、把以下代码添加到我们的style.css文件中:

  1. /* 读者墙 */
  2. .readers-list{font-size:12px;line-height:18px;text-align:left;_zoom:1;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}
  3. .readers-list li{width:230px;float:left;*margin-right:-1px}
  4. .readers-list a,.readers-list a:hover strong{background-color:#f2f2f2;background-image:-webkit-linear-gradient(#f8f8f8,#f2f2f2);background-image:-moz-linear-gradient(#f8f8f8,#f2f2f2);background-image:linear-gradient(#f8f8f8,#f2f2f2)}
  5. .readers-list a{position:relative;display:block;height:36px;margin:4px;padding:4px 4px 4px 44px;color:#999;overflow:hidden;border:#ccc 1px solid;border-radius:2px;box-shadow:#eee 0 0 2px}
  6. .readers-list img,.readers-list em,.readers-list strong{-webkit-transition:all .2s ease-out;-moz-transition:all .2s ease-out;transition:all .2s ease-out}
  7. .readers-list img{width:36px;height:36px;float:left;margin:0 8px 0 -40px;border-radius:2px}
  8. .readers-list em{color:#666;font-style:normal;margin-right:10px}
  9. .readers-list strong{color:#ddd;width:40px;text-align:rightright;position:absolute;rightright:6px;top:4px;font:bold 14px/16px microsoft yahei}
  10. .readers-list a:hover{border-color:#BA4C32;box-shadow:#ccc 0 0 2px;background-color:#fff;background-image:none}
  11. .readers-list a:hover img{opacity:.6;margin-left:0}
  12. .readers-list a:hover em{color:#BA4C32;font:bold 14px/36px microsoft yahei}
  13. .readers-list a:hover strong{color:#BA4C32;rightright:180px;top:0;text-align:center;border-right:#ccc 1px solid;height:44px;line-height:40px}

其中width:230px,可根据实际情况修改,是实现一行显示多少个读者。

3、如果使用的主题也是跟本站一样,是知更鸟的HotNews主题的,可以直接下载本站所使用的留言板模板,然后新建一个页面,选择这个留言板模板即可。文件下载地址(提取码:610a):

下载地址

相关推荐