有細心的網友發現本工作室的教程的瀏覽量統計,重複刷新不增加,留言問怎麼實現的。
本工作室的這個統計代碼還是很久很久很久以前,在某一個插件上面扒下來的代碼,具體什麼插件我也忘了,今天這篇教程也不詳細解析代碼了,直接來懶人模式。
第一步,在你主題的 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,'_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');
- }
- }
第二步:由於我們統計一般只統計文章,所以在你的主題的single.php 文件的最最最最開頭加上如下 php 代碼。
下面的代碼是用來設置 cookie 的,會在用户瀏覽器端增加一個形如: 123a45a45a113 其中字母 a 是分隔文章 ID 的,有效期是一天,設置 cookie 前不能有任何輸出,所以這些代碼要添加在文件的最最開頭。
- $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);
- }
- }
第三步:在 single.php 文件的 while( have_posts() ) : the_post(); 的後面增加統計瀏覽數的函數
- process_postviews();
如下

第四步:輸出
在你需要顯示瀏覽數的地方添加:
- 瀏覽數:<?php the_views(true,$post->ID);?>
好了。