做SEO都知道,在使用wordpress的时候,尽量少用插件,因为插件用的越多,网站打开速度越慢,而网站的打开速度又是影响关键词优化的一个重要因素!
因为很多wordpress网站,会需要展现文章的浏览量,也就是我们常说的阅读量,那么,在这里给大家分享一下,怎么用纯代码显示Wordpress的浏览量!
操作很简单,找到函数模板function.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";
- }
- 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);
- }
- }
然后,我们打开文章页面single.php,找到与下面类似的地方:
- <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
在它的下面添加如下代码
- <?php setPostViews(get_the_ID()); ?>
更后将我们需要展示阅读次数的地方添加如下代码
- <?php echo getPostViews(get_the_ID()); ?>
这样,就可以了,轻松实现纯代码显示Wordpress的浏览量!
- 微信赞赏老师
- 互联网营销培训课程
-
- 苏春宇微信号
- 183 6469 9738(手机)
-