如果你是用WordPress搭建的网站,你是不是想知道你的文章被多少人点击看过?一些用于统计点击次数的插件可以解决这个问题,比如PostViews,可是又怕插件会让网站变慢。今天要推荐的是一个更简单的不用任何插件实现点击数统计的方法。首先,需要创建相关的函数。你可以把下面的代码粘贴到你所用主题的 functions.php
文件里。
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 View"; } return $count.' Views'; } 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); } }
然后,粘贴下面的代码到主题的 single.php
的文章的 loop 里:
<?php setPostViews(get_the_ID()); ?>
最后,把下面的代码拷贝到任意你的主题模板里想要显示点击数的地方:
<?php echo getPostViews(get_the_ID()); ?>
e,,是双倍的增长啊。。。每次咋加了2?
很实用,感谢分享
能解释一下函数的意思嘛。。新手参透不了。。
老大,能否 帮我写个 根据点击数 列出最热门文章(在侧边栏)
当我把 "0 View" 改成 "求围观",和 “ Views”改为“ 热度” 没有点击的时候,还是显示 “0 热度” 的 ,不是应该显示 “求围观”吗
single.php有的模板没有这个文件好吧,不要一概而论啊!误导读者
single.php是主题的文章页,基本上所有模版都有的,这是wp主题的基本页,当然也不排除极个别主题使用了其它名称
觉得PostViews 影响也不是很大,而且PostViews用的人不少