Step 1
By convention, the following code is thrown into functions. php.
The code is as follows: |
Copy code |
/*********** Document statistics *********/ 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 ); // Count all $ Should_count = true; // Exclude the robot $ 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 @ inktom', 'turnitin. com '=> 'turnitinbot', 'technorati '=> 'technorati', 'Yahoo '=> 'Yahoo', 'findexa '=> 'findexa ', 'nextlink' => 'findlink', 'gaais '=> 'gaisbo', 'wisenut' => 'zyborg', 'whoissource' => 'surveybot ', 'bloglines' => 'bloglines', 'blogsearch' => 'blogsearch', 'pubsub '=> 'pubsub', 'syndic8' => 'syndic8 ', 'radiouserland' => 'userland', 'gigabot' => 'gigabot', 'become. com '=> 'become. com ', 'Baidu Bot' => 'baidider Ider '); $ 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 _ totalview ')){ 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 fielm 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 fielm 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 '); } }
|
Step 2
Next, set Cookie
Add the following code at the very beginning of the single. php topic:
The code is as follows: |
Copy code |
<? Php $ COOKNAME = 'ashuwp _ View'; // cookie name $ TIME = 24X3600; $ PATH = '/'; $ Id = $ posts [0]-> ID; $ Expire = time () + $ TIME; // cookie validity period If (isset ($ _ COOKIE [$ COOKNAME]) $ Cookie = $ _ COOKIE [$ COOKNAME]; // Obtain the cookie Else $ Cookie = ''; If (empty ($ cookie )){ // If no cookie exists Setcookie ($ COOKNAME, $ id, $ expire, $ PATH ); } Else { // Use a to split it into arrays $ List = explode ('A', $ cookie ); // If the id of this article already exists If (! In_array ($ id, $ list )){ Setcookie ($ COOKNAME, $ cookie. 'A'. $ id, $ expire, $ PATH ); } } ?>
|
In this code, the Cookie is valid for one day ~
Step 3: www.111cn.net
Continue to modify single. php
Search
While (have_posts (): the_post ();
Add
Process_postviews ();
Step 4
Add the code where you want to display the browser count
Browsing quantity: <? Php the_views (true, $ post-> ID);?>
Add another
1. First, add the following code to functions. php under the topic. This code can also be found online.
The code is as follows: |
Copy code |
// Add by charleswu Function getPostViews ($ postID ){ $ Count_key = 'post _ views_count '; $ Count = get_post_meta ($ postID, $ count_key, true ); If ($ count = ''){ Delete_post_meta ($ postID, $ count_key ); Add_post_meta ($ postID, $ count_key, '0 '); Return "0 "; } Return $ count; } Function setPostViews ($ postID ){ $ Count_key = 'post _ views_count '; $ Count = get_post_meta ($ postID, $ count_key, true ); If ($ count = '') {// www.111cn.net $ Count = 0; Delete_post_meta ($ postID, $ count_key ); Add_post_meta ($ postID, $ count_key, '0 '); } Else { $ Count ++; Update_post_meta ($ postID, $ count_key, $ count ); } }
|
2. To solve the problem of increasing the number of refresh statistics, it must be placed at the top of the article page. It seems that no output can be made before php sets cookies, which hurts. My single. php page
The code is as follows: |
Copy code |
<? Php $ Post_id = get_the_ID (); If (isset ($ _ COOKIE ['view'. $ post_id.COOKIEHASH]) & $ _ COOKIE ['view'. $ post_id.COOKIEHASH] = '1 ') { } Else { SetPostViews ($ post_id ); Setcookie ('view'. $ post_id.COOKIEHASH, '1', time () + 3600, COOKIEPATH, COOKIE_DOMAIN); // Set the interval } ?> |