WordPress纯代码实现文章浏览次数

来自:互联网
时间:2018-08-29
阅读:
免费资源网 - https://freexyz.cn/

将下面代码加入到functiuons.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 );
}
}

将如下代码插入single.php文件中要显示浏览次数的位置

<?php setPostViews(get_the_ID()); ?>
<?php echo getPostViews(get_the_ID()); ?>

需要在哪个页面显示文章的浏览次数,就在相应的模板文件中加入

<?php echo getPostViews(get_the_ID()); ?>
免费资源网 - https://freexyz.cn/
返回顶部
顶部