登录 WordPress 后台,在现有模板的 functions.php 文件中加入如下代码:
/*显示文章浏览次数*/
function getPostViews($postID)
{
$count = get_post_meta($postID, ‘views’, true);
if ($count == ”) {
delete_post_meta($postID, ‘views’);
add_post_meta($postID, ‘views’, ‘0’);
return “0”;
}
return $count . ”;
}
function setPostViews($postID)
{
$count = get_post_meta($postID, ‘views’, true);
if ($count == ”) {
$count = 0;
delete_post_meta($postID, ‘views’);
add_post_meta($postID, ‘views’, ‘0’);
} else {
$count++;
update_post_meta($postID, ‘views’, $count);
}
}
调用方法:
<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?> 效果:每刷新一次这个浏览量就会加1