有細心的網友發現本工作室的教程的瀏覽量統計,重複重新整理不增加,留言問怎麼實現的。

本工作室的這個統計程式碼還是很久很久很久以前,在某一個外掛上面扒下來的程式碼,具體什麼外掛我也忘了,今天這篇教程也不詳細解析程式碼了,直接來懶人模式。

第一步,在你主題的 functions.php 檔案中新增如下程式碼 (是統計計數、獲取瀏覽數的一些函式)

  1. /***********文章統計*********/  
  2. function process_postviews() {   
  3.     global $user_ID$post;   
  4.     if(check_cookie($post))   
  5.         return;   
  6.     if(is_int($post)) {   
  7.         $post = get_post($post);   
  8.     }   
  9.     if(!wp_is_post_revision($post)) {   
  10.         if(is_single() || is_page()) {   
  11.             $id = intval($post->ID);   
  12.             //$post_views = get_post_custom($id);   
  13.             $post_views = get_post_meta($id,'_check_count',true);   
  14.             //統計所有人   
  15.             $should_count = true;   
  16.             //排除機器人   
  17.             $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');   
  18.             $useragent = $_SERVER['HTTP_USER_AGENT'];   
  19.             foreach ($bots as $name => $lookfor) {   
  20.                 if (stristr($useragent$lookfor) !== false) {   
  21.                     $should_count = false;   
  22.                     break;   
  23.                 }   
  24.             }   
  25.             if($should_count) {   
  26.                 if(!update_post_meta($id, '_check_count', ($post_views+1))) {   
  27.                     add_post_meta($id, '_check_count', 1, true);   
  28.                 }   
  29.             }   
  30.         }   
  31.     }   
  32. }   
  33.   
  34. function check_cookie($post){   
  35.     $COOKNAME = 'ashuwp_view';   
  36.     if(isset($_COOKIE[$COOKNAME]))   
  37.         $cookie = $_COOKIE[$COOKNAME];   
  38.     else  
  39.         return false;   
  40.     $id = $post->ID;   
  41.     if(empty($id)){   
  42.         return false;   
  43.     }   
  44.     if(!empty($cookie)){   
  45.         $list = explode('a', $cookie);   
  46.         if(!empty($list) && in_array($id$list)){   
  47.             return true;   
  48.         }   
  49.     }   
  50.     return false;   
  51. }   
  52. ### Function: Display The Post Views   
  53. function the_views($display = true,$id) {   
  54.     $post_views = intval(get_post_meta($id,'_check_count',true));   
  55.     $output = number_format_i18n($post_views);   
  56.     if($display) {   
  57.         echo $output;   
  58.     } else {   
  59.         return $output;   
  60.     }   
  61. }   
  62.   
  63. ### Function: Display Total Views   
  64. if(!function_exists('get_totalviews')) {   
  65.     function get_totalviews($display = true) {   
  66.         global $wpdb;   
  67.         $total_views = intval($wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = '_check_count'"));   
  68.         if($display) {   
  69.             echo number_format_i18n($total_views);   
  70.         } else {   
  71.             return $total_views;   
  72.         }   
  73.     }   
  74. }   
  75.   
  76. ### Function: Add Views Custom Fields   
  77. add_action('publish_post', 'add_views_fields');   
  78. add_action('publish_page', 'add_views_fields');   
  79. function add_views_fields($post_ID) {   
  80.     global $wpdb;   
  81.     if(!wp_is_post_revision($post_ID)) {   
  82.         add_post_meta($post_ID, '_check_count', 0, true);   
  83.     }   
  84. }   
  85. ### Function: Delete Views Custom Fields   
  86. add_action('delete_post', 'delete_views_fields');   
  87. function delete_views_fields($post_ID) {   
  88.     global $wpdb;   
  89.     if(!wp_is_post_revision($post_ID)) {   
  90.         delete_post_meta($post_ID, '_check_count');   
  91.     }   
  92. }  

第二步:由於我們統計一般只統計文章,所以在你的主題的single.php 檔案的最最最最開頭加上如下 php 程式碼。

下面的程式碼是用來設定 cookie 的,會在使用者瀏覽器端增加一個形如: 123a45a45a113 其中字母 a 是分隔文章 ID 的,有效期是一天,設定 cookie 前不能有任何輸出,所以這些程式碼要新增在檔案的最最開頭。

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

第三步:在 single.php 檔案的 while( have_posts() ) : the_post(); 的後面增加統計瀏覽數的函式

  1. process_postviews();  

如下

 

第四步:輸出

在你需要顯示瀏覽數的地方新增:

  1. 瀏覽數:<?php the_views(true,$post->ID);?>  

好了。