• 欢迎访问窗外天空网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站
  • 本站兼容IE9+,谷歌Chrome,火狐Firefox等主流浏览器

WordPress 文章浏览量统计,刷新不重复统计

Wordpress nixon li 438次浏览 0个评论

wordpress 自定义分类用自定义字段文章多重筛选

WordPress 文章浏览量统计,刷新不重复统计

之前在网上看到一片文章可以实现文章浏览量统计而且刷新是不会重复计数.这个功能已经添加到了我网站的主题上来.现在做一次添加的记录.
刷新不重复计数是使用记录cookie来实现的.以下是实现的代码
1.在主题的”functions.php”中添加以下代码

/***********文章阅读统计*********/  
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,'views',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, 'views', ($post_views+1))) {   
                    add_post_meta($id, 'views', 1, true);   
                }   
            }   
        }   
    }   
}   
  
function check_cookie($post){   
    $COOKNAME = 'wp_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,'views',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 = 'views'"));   
        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, 'views', 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, 'views');   
    }   
} 

2.现在我们需要在统计的页面上添加记录cookie的代码,如下:
cookie记录的有效期为一天时间,设置cookie前不能有其他输入,所有代码需要添加在文件最开头.

$COOKNAME = 'wp_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.在需要记录统计浏览的地方添加函数

process_postviews(); 

4.在需要显示浏览数的地方添加以下代码:

ID);?>次浏览

到此浏览计数统计就弄好了.
参考网站:http://www.ashuwp.com/courses/highgrade/586.html


版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明WordPress 文章浏览量统计,刷新不重复统计
喜欢 ( 0)
分享 (0)
关于作者:
咳...咳...
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址