wordpress中无插件完成文章点击阅读次数
笔者在利用WP-PostViews插件时,发现其并不齐全能统计文章阅读情况,有的文章一直是0,因此通过测试,搞到一些不用插件的办法完成文章阅读点击次数。
工具/原料
富文本编辑器,引荐editplus;模板文件。
方法/步骤
1
停用后台WP-PostViews或许相干的阅读统计插件;备份模板文件。
2
在你利用的博客主题里面,找到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 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);
}
}
3
打开single.php,找到以下代码 <?php the_tags('', ', ', ' / '); ?> 在这段代码前面追加以下代码:
<?php setPostViews(get_the_ID()); ?>
<?php echo getPostViews(get_the_ID()); ?>
或许,只需在循环体末尾后的任何地位加上以上代码都是可能的。
4
打开loop.php,找到以下代码 <?php the_tags('', ', ', ' / '); ?> 在这段代码前面追加以下代码:
<?php echo getPostViews(get_the_ID()); ?>
或许,只需在循环体末尾后的任何地位加上以上代码都是可能的。
OK了,到此就完成了不用插件就可能完成阅读次数的效果了。