网上的好多教程都是通过安装插件的方式来添加文章浏览次数,插件多了以后很影像管理后台的性能,今天告诉大家一个方法,通过用户自定义函数来解决文章浏览次数统计功能,找到模板下面的functions.php文件(如果没有的话创建一个),添加如下代码:
/* 获取和设置文章浏览数 */
function getPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if (is_single()) {
if ($count == '') {
add_post_meta($postID, $count_key, 1);
return 1;
} else {
$count ++;
update_post_meta($postID, $count_key, $count);
}
}
return empty($count) ? 0 : $count;
}
函数释意:$postID为文章的ID,在你需要展示浏览次数的地方直接以如下方式调用
<?php echo getPostViews($postID);?>
转载于:https://blog.51cto.com/suiwnet/1591311