WordPress 文章浏览量统计:插件、代码、统计工具 | 全面指南
WordPress 默认情况下不会显示文章的浏览量,不过可以通过以下方法实现:
-
使用插件:可以安装一些插件来显示浏览量,比如 Jetpack、Post Views Counter、WP-PostViews 等。
-
使用代码:可以在主题的 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);
}
}
// 在 single.php 中添加以下代码
setPostViews(get_the_ID());
- 使用第三方统计工具:比如 Google Analytics、百度统计等都可以统计网站的浏览量,并提供相应的数据报告。
原文地址: https://www.cveoy.top/t/topic/lpmu 著作权归作者所有。请勿转载和采集!