1、把下面这段代码加在想显示文章浏览次数的位置(例如模板内的content.php)
<?php setPostViews(get_the_ID()); echo getPostViews(get_the_ID()); ?>
2、如果需要在列表页显示查看次数,那么建议在列表页需要显示查看次数的位置单独添加下面的代码
<?php echo getPostViews(get_the_ID()); ?>
把下面这段代码加在模板文件夹内的functions.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);
}
}
文章来源:https://www.technicalanalysis.cn/
wordpress列表页调用浏览器,wordpress显示文章浏览量!
最新推荐文章于 2023-02-26 22:36:48 发布