欢迎来到站长天空!

WordPress

当前位置: 主页 > CMS教程 > WordPress

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

时间:2025-10-18 16:53:56|栏目:WordPress|点击:

将下面代码加入到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()); ?>


上一篇:WordPress标题标签title的写法

栏    目:WordPress

下一篇:WordPress如何在首页显示所有文章评论

本文标题:WordPress纯代码实现文章浏览次数

本文地址:https://zz.feitang.co/CMSjiaocheng/25073.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 站长天空 版权所有 Powered by EyouCms冀ICP备14023439号