对于博客中评论较多的文章,可以使用如下的函数,查询指定时间范围内的文章,并按照评论数的多少进行查找筛选,输出指定数目的文,即显示热评文章。
- //显示热评文章
- function hot_comment_viewed($number, $days){
- global $wpdb;
- $sql = "SELECT ID , post_title , comment_count
- FROM $wpdb->posts
- WHERE post_type = 'post' AND post_status = 'publish' AND TO_DAYS(now()) - TO_DAYS(post_date) < $days
- ORDER BY comment_count DESC LIMIT 0 , $number ";
- $posts = $wpdb->get_results($sql);
- $i = 1;
- $output = "";
- foreach ($posts as $post){
- $output .= "\n<li><span class='li-icon li-icon-$i'>$i</span><a href= \"".get_permalink($post->ID)."\" rel=\"bookmark\" title=\"".$post->post_title." (".$post->comment_count."条评论)\" >".$post->post_title."</a></li>";
- $i++;
- }
- echo $output;
- }
在调用时就可以使用如下代码实现功能了,输出最近 100 天内评论数排序前 8 的文章:
- <div id="hot_comment_widget">
- <ul>
- <?php hot_comment_viewed(8, 600); ?>
- <?php wp_reset_query(); ?>
- </ul>
- </div>