wordpress进阶教程(三十六):文章浏览量统计-重复刷新不增加
有细心的网友发现本工作室的教程的浏览量统计,重复刷新不增加,留言问怎么实现的。
本工作室的这个统计代码还是很久很久很久以前,在某一个插件上面扒下来的代码,具体什么插件我也忘了,今天这篇教程也不详细解析代码了,直接来懒人模式。
第一步,在你主题的functions.php文件中添加如下代码(是统计计数、获取浏览数的一些函数)
- /***********文章统计*********/
- process_postviews() {
- $user_ID, $post;
- (check_cookie($post))
- ;
- (is_int($post)) {
- $post = get_post($post);
- }
- (!wp_is_post_revision($post)) {
- (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 = (‘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’];
- ($bots $name => $lookfor) {
- (stristr($useragent, $lookfor) !== false) {
- $should_count = false;
- ;
- }
- }
- ($should_count) {
- (!update_post_meta($id, ‘_check_count’, ($post_views+1))) {
- add_post_meta($id, ‘_check_count’, 1, true);
- }
- }
- }
- }
- }
- check_cookie($post){
- $COOKNAME = ‘ashuwp_view’;
- (isset($_COOKIE[$COOKNAME]))
- $cookie = $_COOKIE[$COOKNAME];
- false;
- $id = $post->ID;
- (($id)){
- false;
- }
- (!($cookie)){
- $list = explode(‘a’, $cookie);
- (!($list) && in_array($id, $list)){
- true;
- }
- }
- false;
- }
- ### Function: Display The Post Views
- the_views($display = true,$id) {
- $post_views = intval(get_post_meta($id,’_check_count’,true));
- $output = number_format_i18n($post_views);
- ($display) {
- echo $output;
- } {
- $output;
- }
- }
- ### Function: Display Total Views
- (!function_exists(‘get_totalviews’)) {
- get_totalviews($display = true) {
- $wpdb;
- $total_views = intval($wpdb->get_var(“SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = ‘_check_count'”));
- ($display) {
- echo number_format_i18n($total_views);
- } {
- $total_views;
- }
- }
- }
- ### Function: Add Views Custom Fields
- add_action(‘publish_post’, ‘add_views_fields’);
- add_action(‘publish_page’, ‘add_views_fields’);
- add_views_fields($post_ID) {
- $wpdb;
- (!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’);
- delete_views_fields($post_ID) {
- $wpdb;
- (!wp_is_post_revision($post_ID)) {
- delete_post_meta($post_ID, ‘_check_count’);
- }
- }
第二步:由于我们统计一般只统计文章,所以在你的主题的single.php文件的最最最最开头加上如下php代码。
下面的代码是用来设置cookie的,会在用户浏览器端增加一个形如: 123a45a45a113 其中字母a是分隔文章ID的,有效期是一天,设置cookie前不能有任何输出,所以这些代码要添加在文件的最最开头。
- $COOKNAME = ‘ashuwp_view’; //cookie名称
- $TIME = 3600 * 24;
- $PATH = ‘/’;
- $id = $posts[0]->ID;
- $expire = time() + $TIME; //cookie有效期
- (isset($_COOKIE[$COOKNAME]))
- $cookie = $_COOKIE[$COOKNAME]; //获取cookie
- $cookie = ”;
- (($cookie)){
- //如果没有cookie
- setcookie($COOKNAME, $id, $expire, $PATH);
- }{
- //用a分割成数组
- $list = explode(‘a’, $cookie);
- //如果已经存在本文的id
- (!in_array($id, $list)){
- setcookie($COOKNAME, $cookie.’a’.$id, $expire, $PATH);
- }
- }
第三步:在single.php文件的 while( have_posts() ) : the_post();的后面增加统计浏览数的函数
- process_postviews();
如下
第四步:输出
在你需要显示浏览数的地方添加:
- 浏览数:<?php the_views(true,$post->ID);?>
好了。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END