国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > WordPress > WordPress不用插件调用指定时间内热门文章

WordPress不用插件调用指定时间内热门文章

来源:程序员人生   发布时间:2014-03-18 11:01:50 阅读次数:2628次

之前在网上找到的一些有关WordPess热门文章都是根据评论数多少来调用的,虽然安装的WP-PostViews Plus这个统计文件浏览数插件带有调用文章功能,但却没有办法规定时间段,比如7天之内还是一个月之内。

使用下面代码的前提条件是你必须安装有WP-PostViews Plus这款插件,可更改代码中的'-7 days'这个7数字来获取多天内的热门点击文章。

function most_viewed($mode = '', $limit = 10, $chars = 0, $display = true) {
global $wpdb, $post;
$views_options = get_option('PVP_options');
$where = '';
$temp = '';
$output = '';
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 > '" . date('Y-m-d', strtotime('-7 days')) . "' 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_views = intval($post->views);
$post_title = get_the_title();
if($chars > 0) {
$post_title = snippet_text($post_title, $chars);
}
// Downloads By http://www.veryhuo.com
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
$post_content = get_the_content();
$temp = stripslashes($views_options['most_viewed_template']);
$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp);
$temp = str_replace("%POST_TITLE%", $post_title, $temp);
$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp);
$temp = str_replace("%POST_CONTENT%", $post_content, $temp);
$temp = str_replace("%POST_URL%", get_permalink(), $temp);
$output .= $temp;
//$output=stripslashes($views_options['most_viewed_template']);
}
} else {
$output = '<li>N/A</li>'."";
}
if($display) {
echo $output;
} else {
return $output;
}
}

讲上面的代码放到你主题下的Function.php文件中,在须要调用的地方通过下面的代码调用即可,其中10表示调用10篇文章,可根据你的需要自定义调用多少篇。

<?php
if (function_exists('get_most_viewed') & function_exists('most_viewed')){
most_viewed('post',10);
}
?>

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生