增强型wordpress文章浏览量统计,支持重复刷新不增加

WordPress教程 2215

这是由阿树工作室扒自某款插件,支持重复刷新不增加wordpress文章浏览量统计的代码,相对于博客吧前面分享的刷新不累加的wordpress文章浏览次数统计功能的教程代码,功能更加完整,代码更加完善,支持统计所有人的浏览和排除机器人的浏览量,有兴趣的博主可以参考或直接采用,懒人博主则可以直接使用wp-postviews插件

教程步骤:

1、在当前主题的functions.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/***********文章统计*********/  
function process_postviews() {   
	global $user_ID, $post;   
	if(check_cookie($post))   
		return;   
	if(is_int($post)) {   
		$post = get_post($post);   
	}   
	if(!wp_is_post_revision($post)) {   
		if(is_single() || is_page()) {   
			$id = intval($post->ID);   
			//$post_views = get_post_custom($id);   
			$post_views = get_post_meta($id,'_check_count',true);   
			//统计所有人   
			$should_count = true;   
			//排除机器人   
			$bots = array('Google Bot' => 'googlebot', 'Google Bot' => 'google', 'MSN' => 'msnbot', 'Alex' => 'ia_archiver', 'Lycos' => 'lycos', 'Ask Jeeves' => 'jeeves', 'Altavista' => 'scooter', 'AllTheWeb' => 'fast-webcrawler', 'Inktomi' => 'slurp@inktomi', 'Turnitin.com' => 'turnitinbot', 'Technorati' => 'technorati', 'Yahoo' => 'yahoo', 'Findexa' => 'findexa', 'NextLinks' => 'findlinks', 'Gais' => 'gaisbo', 'WiseNut' => 'zyborg', 'WhoisSource' => 'surveybot', 'Bloglines' => 'bloglines', 'BlogSearch' => 'blogsearch', 'PubSub' => 'pubsub', 'Syndic8' => 'syndic8', 'RadioUserland' => 'userland', 'Gigabot' => 'gigabot', 'Become.com' => 'become.com','Baidu Bot'=>'Baiduspider');   
			$useragent = $_SERVER['HTTP_USER_AGENT'];   
			foreach ($bots as $name => $lookfor) {   
				if (stristr($useragent, $lookfor) !== false) {   
					$should_count = false;   
					break;   
				}   
			}   
			if($should_count) {   
				if(!update_post_meta($id, '_check_count', ($post_views+1))) {   
					add_post_meta($id, '_check_count', 1, true);   
				}   
			}   
		}   
	}   
}   
 
function check_cookie($post){   
	$COOKNAME = 'ashuwp_view';   
	if(isset($_COOKIE[$COOKNAME]))   
		$cookie = $_COOKIE[$COOKNAME];   
	else  
		return false;   
	$id = $post->ID;   
	if(empty($id)){   
		return false;   
	}   
	if(!empty($cookie)){   
		$list = explode('a', $cookie);   
		if(!empty($list) && in_array($id, $list)){   
			return true;   
		}   
	}   
	return false;   
}   
### Function: Display The Post Views   
function the_views($display = true,$id) {   
	$post_views = intval(get_post_meta($id,'_check_count',true));   
	$output = number_format_i18n($post_views);   
	if($display) {   
		echo $output;   
	} else {   
		return $output;   
	}   
}   
 
### Function: Display Total Views   
if(!function_exists('get_totalviews')) {   
	function get_totalviews($display = true) {   
		global $wpdb;   
		$total_views = intval($wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = '_check_count'"));   
		if($display) {   
			echo number_format_i18n($total_views);   
		} else {   
			return $total_views;   
		}   
	}   
}   
 
### Function: Add Views Custom Fields   
add_action('publish_post', 'add_views_fields');   
add_action('publish_page', 'add_views_fields');   
function add_views_fields($post_ID) {   
	global $wpdb;   
	if(!wp_is_post_revision($post_ID)) {   
		add_post_meta($post_ID, '_check_count', 0, true);   
	}   
}   
### Function: Delete Views Custom Fields   
add_action('delete_post', 'delete_views_fields');   
function delete_views_fields($post_ID) {   
	global $wpdb;   
	if(!wp_is_post_revision($post_ID)) {   
		delete_post_meta($post_ID, '_check_count');   
	}   
}

2、一般只统计文章的浏览量,所以把下面的代码添加到当前主题single.php文件的第一行,代码作用是:用来设置cookie,会在用户浏览器端增加一个形如: 123a45a45a113 其中字母a是分隔文章ID的,有效期是一天,由于设置cookie前不能有任何输出,所以这些代码要添加在文件的最最开头。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$COOKNAME = 'ashuwp_view'; //cookie名称   
$TIME = 3600 * 24;   
$PATH = '/';   
 
$id = $posts[0]->ID;   
$expire = time() + $TIME; //cookie有效期   
if(isset($_COOKIE[$COOKNAME]))   
	$cookie = $_COOKIE[$COOKNAME]; //获取cookie   
else  
	$cookie = '';   
 
if(empty($cookie)){   
	//如果没有cookie   
	setcookie($COOKNAME, $id, $expire, $PATH);   
}else{   
	//用a分割成数组   
	$list = explode('a', $cookie);   
	//如果已经存在本文的id   
	if(!in_array($id, $list)){   
		setcookie($COOKNAME, $cookie.'a'.$id, $expire, $PATH);   
	}   
}

3、再在single.php文件的主循环部分(while( have_posts() ) : the_post();)后面自己喜欢的位置添加函数调用代码:

1
process_postviews();

4、在要显示浏览数的地方添加调用代码:

1
<?php the_views(true,$post->ID);?>

保存文件即可。

原文地址:http://www.ashuwp.com/courses/highgrade/586.html

精品推荐: