答案:可通过代码或轻量插件实现WordPress文章浏览统计。方法一将统计函数添加到functions.php并调用显示;方法二安装Post Views Counter等插件,用短代码或模板标签展示浏览量,建议使用子主题避免更新丢失。

在WordPress中添加文章浏览次数统计功能,不需要依赖复杂插件也能实现。可以通过代码方式或使用轻量插件来完成,以下提供两种实用方法。
方法一:使用代码手动添加浏览统计
这种方法适合希望减少插件数量、提升网站性能的用户。将以下代码添加到主题的 functions.php 文件中:
function set_post_views($post_id) {$count_key = 'post_views_count';
$count = get_post_meta($post_id, $count_key, true);
if ($count == '') {
$count = 0;
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
} else {
$count++;
update_post_meta($post_id, $count_key, $count);
}
}
// 获取浏览数
function get_post_views($post_id) {
$count_key = 'post_views_count';
$count = get_post_meta($post_id, $count_key, true);
if ($count == '') {
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
return "0 次查看";
}
return $count . ' 次查看';
}
// 排除管理员查看计数(可选)
function track_post_views($post_id) {
if (!is_single()) return;
if (empty($post_id)) {
global $post;
$post_id = $post->ID;
}
if (!is_admin() && !current_user_can('manage_options')) {
set_post_views($post_id);
}
}
add_action('wp', 'track_post_views');
保存后,在文章模板文件(如 single.php 或内容循环内)插入以下调用代码显示浏览次数:
方法二:使用轻量插件快速实现
如果你不熟悉代码操作,推荐使用成熟插件,比如 Post Views Counter 或 WP-PostViews。
- 进入WordPress后台 → “插件” → “安装插件”
- 搜索 Post Views Counter
- 安装并启用插件
- 插件会自动为每篇文章统计浏览量
- 在文章页面通过短代码或模板标签显示数据,例如:
[post-views] 或
注意事项
手动添加代码时,建议使用子主题,避免主题更新导致代码丢失。另外,统计功能不会记录重复刷新(同一用户短时间内多次访问),但可通过插件设置调整精度。
基本上就这些,实现起来不复杂但容易忽略细节。










