要用插件就不去说了,Wp-PostViews,自己去安装下
下面介绍手动修改Wordpress代码实现添加浏览次数(views)统计功能
首先进入你的Wordpress主题目录下,找到functions.php文件,在适当位置插入下面代码
/* Postviews start */ 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==''){ $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); }else{ $count++; update_post_meta($postID, $count_key, $count); } } /* Postviews start end*/
然后在single.php中的 endwhile; endif; wp_reset_query(); 循环前添加如下代码:
<?php setPostViews(get_the_ID());?>
最后在你想要显示文章浏览次数统计的地方(一般在index.php、sidebar.php或single.php文件) 添加如下代码即可。
<?php echo getPostViews(get_the_ID()); ?> 次浏览