一个分享WordPress、Zblog、Emlog、Typecho等主流博客的教程网站!
当前位置:网站首页 > 博客教程 > WordPress教程 > 正文

wordpress插件wp-postveiws调用限定时间内热门文章

作者:xlnxin发布时间:2021-04-15分类:WordPress教程浏览:458


导读:目前首页侧栏调用的是30天内浏览次数最多的文章,通过wp-postviews插件的get_timespan_most_viewed函数,wp-postviews插件1.3...

目前首页侧栏调用的是30天内浏览次数最多的文章,通过wp-postviews插件的get_timespan_most_viewed函数,wp-postviews插件1.30版本后取消了该函数,但有网友共享了原来的函数,现在只需要把该函数的相关代码粘贴到插件的wp-postveiws.php文件再进行调用即可。

操作步骤:

把下面的函数代码复制粘贴到wp-postviews插件的wp-postveiws.php文件(直接放到文件最底部即可):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
### Function: Display timespan most Viewed Page/Post
function get_timespan_most_viewed($mode = '', $limit = 10, $chars = 0,$days = 30/*设置天数*/, $display = true) {
    global $wpdb;
    $limit_date = current_time('timestamp') - ($days*86400);
    $limit_date = date("Y-m-d H:i:s",$limit_date);
    $views_options = get_option('views_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 < '".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_views = intval($post->views);
			$post_title = get_the_title($post);
			if($chars > 0) {
				$post_title = snippet_text($post_title, $chars);
			}
			$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
			$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->post_content, $temp);
			$temp = str_replace("%POST_URL%", get_permalink($post), $temp);
			$output .= $temp;
		}	
    } else {
		$output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
    }
    if($display) {
		echo $output;
    } else {
		return $output;
    }
}

在要显示热门文章的地方添加调用代码:

1
<?php get_timespan_most_viewed('post',14);?>

表示显示14篇30天之内浏览次数最多的日志文章

标签:wordpress插件mysql