今天在做自己的博客時希望調用指定時間段內的熱門文件或評論最多的文章,自己不怎么會寫程序于是搜索了關于調用最熱與評論最多的文件方法,現在我們找到的方法分享給大家.
“某段時間內最熱文章”,就是指自定義一段時間內的文章中評論最多的文章,以前很多人用的是全部文章的最熱文章功能,用處不大.
某段時間內的最熱文章也很多人寫過吧,具體就記不清了,我這里也貼一下自己修改的.
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篇.
新聞熱點
疑難解答
圖片精選