However, it is really not cost-effective to use a plug-in to implement such a simple function. After a hard job, we can get a code that supports caching, and also use Ajax.
The first is the code:
| The code is as follows: |
Copy code |
|
Function Bing_statistics_visitors ($ cache = false ){
Global $ post;
$ Id = $ post-> ID;
If ($ cache) $ id = $ _ GET ['id'];
If ((! Is_singular ()&&! $ Cache) |! $ Id) return;
If (WP_CACHE &&! $ Cache ){
Echo '<script type = "text/javascript"> window. onload = function () {var e = null; window. XMLHttpRequest? E = new XMLHttpRequest: e = new ActiveXObject ("Microsoft. XMLHTTP "), e. open ("GET ","'. admin_url ('admin-ajax. php '). '",! 0), e. send ("action = visitors & id = '. $ id.'")} </script> ';
Return;
}
$ Post_views = (int) get_post_meta ($ id, 'view', true );
If (! Update_post_meta ($ id, 'view', ($ post_views + 1) add_post_meta ($ id, 'view', 1, true );
}
Add_action ('WP _ head', 'Bing _ statistics_visitors ');
// Solve the cache problem
Function Bing_statistics_cache (){
Bing_statistics_visitors (true );
}
Add_action ('WP _ ajax_nopriv_visitors ', 'Bing _ statistics_cache ');
Add_action ('WP _ ajax_visitors ', 'Bing _ statistics_cache ');
// Obtain the count
Function Bing_get_views ($ get = true ){
Global $ post;
$ Views = number_format (int) get_post_meta ($ post-> ID, 'view', true ));
If ($ get) return $ views;
Echo $ views;
} |
When the code enables statistics, it will automatically output a small JS for Ajax. If you have enabled caching, do not forget to delete all cached files when using my code.
Use the Bing_get_views () function to obtain or output the quantity:
| The code is as follows: |
Copy code |
|
// Obtain
Echo Bing_get_views ();
// Print directly
Bing_get_views (false );
|
In addition, I added a statistical code for calling php pages like js.
The document template contains the following statements:
| The code is as follows: |
Copy code |
|
<SCRIPT src = "counter. asp? ArticleId = <% = # articleId # %> "> </SCRIPT>
The counter. asp file is an asp file for data recording.
<%
'###################
#########
'By Wang Xiangchao
'###################
Dim articleId, sqlStr, hits
ArticleId = int (trim (request. querystring ("articleId ")))
SqlStr = "update articles set hits = hits 1 where articleId =" & articleId
'Add 1 to the article clicks
Conn.exe cute (sqlStr)
'Number of clicks on read articles
Hitsaskconn.exe cute ("select hits from articles where articleId =" & articleId) (0)
%>
'Print the number of clicks on the article
Document. write (<% = hits %>) |