今天在做自己的博客时希望调用指定时间段内的热门文件或评论最多的文章,自己不怎么会写程序于是搜索了关于调用最热与评论最多的文件方法,现在我们找到的方法分享给大家.
“某段时间内最热文章”,就是指自定义一段时间内的文章中评论最多的文章,以前很多人用的是全部文章的最热文章功能,用处不大.
某段时间内的最热文章也很多人写过吧,具体就记不清了,我这里也贴一下自己修改的.
1.把下面的函数代码扔到主题的 functions.php 文件里面,具体看注释,代码如下:
- function most_comm_posts($days=7, $nums=10) { //$days参数限制时间值,单位为‘天’,默认是7天;$nums是要显示文章数量
- global $wpdb;
- $today = date("Y-m-d H:i:s"); //获取今天日期时间
- $daysago = date( "Y-m-d H:i:s", strtotime($today) - ($days * 24 * 60 * 60) ); //Today - $days
- $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 , $nums");
- $output = '';
- if(emptyempty($result)) {
- $output = '<li>None data.</li>';
- } else {
- foreach ($result as $topten) {
- $postid = $topten->ID;
- $title = $topten->post_title;
- $commentcount = $topten->comment_count;
- if ($commentcount != 0) {
- $output .= '<li><a href="'.get_permalink($postid).'" title="'.$title.'">'.$title.'</a> ('.$commentcount.')</li>';
- }
- }
- }
- echo $output;
- }
2.调用方法,例如放在侧边栏:
- <h3>近期最热文章</h3>
- <ul>
- 代码如下 复制代码
- <?php if(function_exists('most_comm_posts')) most_comm_posts(30, 10); ?>
- </ul>
提示:函数参数1是按天计算的,30就是30天,参数2是文章显示数量,10就是显示10篇.
新闻热点
疑难解答
图片精选