在另一个文章中我提醒过,使用代码替换WP-Postviews 插件实现文章浏览次数会从0开始,那保留WP-Postviews 插件浏览数据的方法,下面给大家分享一下。
问题根源:
解决问题之前,我们先看下那段代码实现的原理: 它是通过在数据库中根据postID 文章ID,然后对每个postID都新建一个“post_views_count”字段,然后使用“get_post_meta”函数获得“post_views_count”字段的数值(即为文章浏览数),最后通过“update_post_meta”函数来为每次浏览+1并且记录到“post_views_count”字段里面,从而实现了文章浏览数统计。
那使用代码替换WP-Postviews 插件实现文章浏览次数会从0开始是什么原因呢? 很简单,因为WP-Postviews 插件是使用自己定义的字段“views”,自然是互补干扰,各显示各的!
解决方法:
解决方法很简单,我们改进一下原有代码,让它直接显示“views”字段的参数即可! 改进后代码分享://读取浏览量,获得Wordpress文章浏览次数 function getPostViews($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, 'views', true); if($count==''){ delete_post_meta($postID,'views'); add_post_meta($postID, 'views', '0'); return "0"; } return $count.'views';} //设置文章浏览数,进行统计 function setPostViews($postID) { //$count_key = 'post_views_count'; $count = get_post_meta($postID, 'views', true); if($count==''){<br /> $count = 0; delete_post_meta($postID, 'views'); add_post_meta($postID, 'views', '0'); }else{ $count++; update_post_meta($postID, 'views', $count); } } //获取浏览量 function the_views(){ echo getPostViews(get_the_ID()); echo ""; if (is_single()) { setPostViews(get_the_ID()); } }使用方法和上一篇文章介绍的一致:
1、使用Sublime或者Notepad++打开functions.php,在最后面的 ?>之前添加上面的代码就行。 2、使用Sublime或者Notepad++打开single.php,添加下面的代码在主循环(loop)之内。
<?php setPostViews(get_the_ID()); ?>(小技巧:搜索标题显示函数调用代码<?php the_title(); ?>,把下面的代码添加在此代码的后面就可以了) 例如我的:

<?php if(function_exists(‘the_views’)) { the_views(); } ?>
eg:比如我的

如果没用过WP-Postviews插件,请参考另一篇实现代码:
《不用WP-Postviews 插件使用简单代码实现文章浏览次数的显示功能》
最后提醒一下大家,如果你想让WP-Postviews 插件保留浏览数统计数据的话,直接在ftp里面把插件删除即可,千万不要使用WP后台插件的删除功能,那会清空所有浏览数统计数据。
如图:直接在plugins中找到 WP-Postviews 插件删除(这里我已经删除了,所以没有)。
关于上一篇新增加的刷新不重复增加浏览量代码替换后浏览量清零的问题的解决方法
问题的本质是一样的,在统计代码中将_check_count替换为views即可,把原有的代码替换为如下代码就能保存原来的浏览量数据。
/*文章浏览量统计(刷新无重复版,并且保留原有浏览数据)*/ 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,'views',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, 'views', ($post_views+1))) { add_post_meta($id, 'views', 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,'views',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 = 'views'")); 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, 'views', 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, 'views'); } }