虽然WP-Postviews是个很不错的WordPress浏览次数统计插件,但是很多朋友喜欢插件能少则少.
那么如何不用插件实现浏览次数统计.
步骤一:打开当前模板的functions.php文件.
在<?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);
}
}
里面的阅读可以按自己的喜好,改成自己的风格.如热度,围观等 都可以.
如下图所示:
步骤二: 在需要显示浏览次数的位置 放入:
阅读(<?php echo getPostViews(get_the_ID()); ?>)
如下图所示:
然后的显示效果如下图:
如果要刷新网页不增加点击量,请参考: WordPress刷新网页不增加网站浏览量点击量统计的代码