WordPress显示一周热评

WordPress站点如何显示一周热评,我相信还是有很多人都想要知道的,那么这里就简单的说说实现方法了。

代码方面的思路其实也是很简单的事情,如果说要统计的话,那么必然要接触到数据库,如果还要给一个时间限制的话,那么同样的也是需要时间函数加上时间戳的。

具体代码如下:

<?php
$days = 7; //七天的时间戳
$today = date("Y-m-d H:i:s"); //今日日期
$daysago = date("Y-m-d H:i:s",strtotime(date('Y-m-j H:i:s')) - (7 * 24 * 60 * 60));  //以今天为基础的前七天

$result = $wpdb->get_results("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN $daysago AND $today ORDER BY comment_count DESC LIMIT 0 , 10");//显示数量为10条
 
foreach ($result as $topten) {
    $postid = $topten->ID;
    $title = $topten->post_title;
    $commentcount = $topten->comment_count;
    if ($commentcount != 0) {
    ?>
         <li><a href="<?php echo get_permalink($postid); ?>"><?php echo $title ?></a></li>
    <?php } 
} 
?>

那么整个代码就是上面的,我个人觉得你可以用在小工具栏上面,将是一个不错的选择。