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. 報表了!說道:

    熱門很有利于閱讀

發表評論

電子郵件地址不會被公開。 必填項已用*標注

*

LOL外围 官网竞博| JBO电竞| 竞博电竞| 竞博| 电竞竞博| 竞博| 竞博JBO| 竞博官网下载| 竞博官网| JBO| 竞博体育| JBO体育| 竞博官网| JBO官网|