WordPress文章刷新不影响浏览量点击量统计代码

文章目录

目前 wordpress 系统建站已经非常流行,占据了建设系统的半壁江山,无非是 WordPress 功能强大开发方便扩展性极强,但是 wordpress 程序默认是没有文章浏览器统计,这点就很鸡肋。之前我们有给大家分享过,纯代码为 wordpress 添加文章浏览量统计功能,但是无论是用纯代码还是使用 wordpress 插件 wp-postview,当我们刷新当前页面会算作一次浏览数量,那今天大家给大家介绍下刷新不重复记录的文章浏览量统计代码!

方法一

一、在 wordpress 主题下 functions.php 里增加以下代码:

  1. //add by charleswu
  2. function getPostViews($postID) {
  3.     $count_key = 'post_views_count';
  4.     $count = get_post_meta($postID, $count_key, true);
  5.     if ($count == '') {
  6.         delete_post_meta($postID, $count_key);
  7.         add_post_meta($postID, $count_key, '0');
  8.         return "0";
  9.     }
  10.     return $count;
  11. }
  12. function setPostViews($postID) {
  13.     $count_key = 'post_views_count';
  14.     $count = get_post_meta($postID, $count_key, true);
  15.     if ($count == '') {
  16.         $count = 0;
  17.         delete_post_meta($postID, $count_key);
  18.         add_post_meta($postID, $count_key, '0');
  19.     } else {
  20.         $count++;
  21.         update_post_meta($postID, $count_key, $count);
  22.     }
  23. }

二、解决刷新统计数增加,一定要放在文章页面的最前面,貌似 php 设置 cookie 之前不能有输出,蛋疼。我的是 single.php 页面:

  1. <?php
  2.     $post_id=get_the_ID();
  3.     if(isset($_COOKIE['views'.$post_id.COOKIEHASH]) && $_COOKIE['views'.$post_id.COOKIEHASH] == '1')
  4.     {
  5.  
  6.     }
  7.     else{    
  8.         setPostViews($post_id);
  9.         setcookie('views'.$post_id.COOKIEHASH,'1',time() + 3600,COOKIEPATH,COOKIE_DOMAIN);//设置时间间隔
  10.     }
  11. ?>

其实第一段代码和网上能找到的普通 wordpress 文章浏览量添加代码是一样的,重点是第二段的需要在文章页面 single 里面添加的这段代码起着决定性的作用。

方法二

此方法扒自某款插件,支持重复刷新不增加 wordpress 文章浏览量统计的代码,相对于博客吧前面分享的刷新不累加的 wordpress 文章浏览次数统计功能的教程代码,功能更加完整,代码更加完善,支持统计所有人的浏览和排除机器人的浏览量,有兴趣的博主可以参考或直接采用,懒人博主则可以直接使用 wp-postviews 插件。
1、在当前主题的 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. }

2、一般只统计文章的浏览量,所以把下面的代码添加到当前主题 single.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. }

3、再在 single.php 文件的主循环部分(while( have_posts() ) : the_post();)后面自己喜欢的位置添加函数调用代码:

  1. process_postviews();

4、在要显示浏览数的地方添加调用代码:

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

两种方法基本雷同,但是感觉第一种更简单点,具体那种方法更受欢迎更实用还需要大家的测试评估,这就是增强型 wordpress 文章浏览量统计支持重复刷新不增加计数的全部内容,需要大家能够用到不枉我们收集整理!

© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
相关推荐
  • 暂无相关文章
评论 抢沙发