Question to say "I can!"

让WP-Postviews显示近期的热门文章

2010-10-29

说来惭愧,一年前就想实现这个功能了,至到今天才弄明白,效果可以看我的热文

从WP-Postviews 1.30版本后就取消了一个补充函数get_timespan_most_viewed,这个函数可以显示特定时间内的热门文章,刚好是我想要的。只要把这个函数重新拷贝到WP-Postviews插件的wp-postviews.php文件内就可以使用了。

### Function: Get TimeSpan Most Viewed
function get_timespan_most_viewed($mode = '', $limit = 20, $days = 30, $display = true) {
global $wpdb, $post;
$limit_date = current_time('timestamp') - ($days*86400);
$limit_date = date("Y-m-d H:i:s",$limit_date);
$where = '';
$temp = '';
if(!empty($mode) && $mode != 'both') {
$where = "post_type = '$mode'";
} else {
$where = '1=1';
}
$most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND post_date > '".$limit_date."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER  BY views DESC LIMIT $limit");
if($most_viewed) {
foreach ($most_viewed as $post) {
$post_title = get_the_title();
$post_views = intval($post->views);
$post_views = number_format($post_views);
$temp .= "<li><a href=\"".get_permalink()."\">$post_title</a> - $post_views ".__('views', 'wp-postviews')."</li>";
}
} else {
$temp = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
}
if($display) {
echo $temp;
} else {
return $temp;
}
}

$days这个参数是多长时间内发表的文章,我设置为30天,就是1个月的热门文章。

方法参考了:http://www.caxblog.com/get-timespan-most-viewed-wp-postviews/



作者:admin | Categories:设计问题 | Tags:

2条评论

  1. 报表了!说道:

    热门很有利于阅读

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*

无觅相关文章插件,快速提升流量