首页 » 调用特定时间内评论最多文章

调用特定时间内评论最多文章

首先把下列代码放入 functions.php


/* 某段时间内最热文章
 * Reference: http://www.wprecipes.com/rarst-asked-how-to-get-most-commented-posts-of-the-week
 * Edit: zwwooooo
 */
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(empty($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;
}

然后在需要的地方调用函数,30, 10 分别指日期(过去30天内)和显示文章数(10篇)。

<h3>近期最热文章</h3>
<ul>
   <?php if(function_exists('most_comm_posts')) most_comm_posts(30, 10); ?>
</ul>

代码来源:ZWW.Me

Derek Yang
我是苹果和 Google 的用户;偶尔我行我素的 80 后。我有一条帅气的金毛,它的名字叫 Lele;Lele 两个月的时候生过一场病、三个月的时候出过一次车祸、现在很淘气。

还没有评论

添加评论

 

— required *

— required *