完整代码(添加到主题的 functions.php 中)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* AJAX 浏览量统计系统 - 完整版
* 功能:无冲突统计、手动修改、插件兼容、实时显示
*/

// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}

/* ===== 核心功能函数 ===== */

/**
* 获取文章浏览次数
*/
function getPostViews($postID) {
// 优先使用 Post Views Counter 插件
if (function_exists('pvc_get_post_views')) {
$count = pvc_get_post_views($postID);
}
// 后备使用自定义字段
else {
$count = get_post_meta($postID, 'views', true);
if ($count == '') {
delete_post_meta($postID, 'views');
add_post_meta($postID, 'views', '0');
$count = 0;
}
}
return $count . '';
}

/**
* 手动设置浏览量
*/
function manualSetPostViews($postID, $view_count) {
$view_count = intval($view_count);

// 更新 Post Views Counter 插件
if (function_exists('pvc_update_post_views_count')) {
pvc_update_post_views_count($postID, $view_count);
}

// 更新自定义字段
update_post_meta($postID, 'views', $view_count);
}

/* ===== AJAX 浏览量统计 ===== */

/**
* 注册 AJAX 处理函数
*/
function register_ajax_post_views() {
add_action('wp_ajax_count_post_views', 'handle_count_post_views');
add_action('wp_ajax_nopriv_count_post_views', 'handle_count_post_views');
}
add_action('init', 'register_ajax_post_views');

/**
* 处理 AJAX 浏览量统计请求
*/
function handle_count_post_views() {
// 安全检查
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'post_views_nonce')) {
wp_die('安全验证失败');
}

if (isset($_POST['post_id'])) {
$post_id = intval($_POST['post_id']);

// 验证文章存在
if (!get_post($post_id)) {
wp_die('文章不存在');
}

// 使用 Post Views Counter 插件
if (function_exists('pvc_view_post')) {
pvc_view_post($post_id);
$new_count = pvc_get_post_views($post_id);
}
// 后备使用自定义字段
else {
$count = get_post_meta($post_id, 'views', true);
if ($count == '') {
$count = 1;
update_post_meta($post_id, 'views', $count);
} else {
$count = intval($count) + 1;
update_post_meta($post_id, 'views', $count);
}
$new_count = $count;
}

// 返回更新后的浏览量
wp_send_json_success(array(
'new_count' => number_format($new_count),
'raw_count' => $new_count
));
}

wp_die();
}

/**
* 在文章页面输出 AJAX 计数脚本
*/
function ajax_post_views_counter() {
if (is_single()) {
$post_id = get_the_ID();
$nonce = wp_create_nonce('post_views_nonce');
?>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0A%20%20%20%20%20%20%20%20jQuery(document).ready(function(%24)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20%E5%8F%91%E9%80%81%20AJAX%20%E8%AF%B7%E6%B1%82%E7%BB%9F%E8%AE%A1%E6%B5%8F%E8%A7%88%E9%87%8F%0A%20%20%20%20%20%20%20%20%20%20%20%20%24.ajax(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20url%3A%20'%3C%3Fphp%20echo%20admin_url('admin-ajax.php')%3B%20%3F%3E'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20type%3A%20'POST'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20data%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20action%3A%20'count_post_views'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20post_id%3A%20'%3C%3Fphp%20echo%20%24post_id%3B%20%3F%3E'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nonce%3A%20'%3C%3Fphp%20echo%20%24nonce%3B%20%3F%3E'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20success%3A%20function(response)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(response.success)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20%E5%AE%9E%E6%97%B6%E6%9B%B4%E6%96%B0%E9%A1%B5%E9%9D%A2%E4%B8%8A%E7%9A%84%E6%B5%8F%E8%A7%88%E9%87%8F%E6%98%BE%E7%A4%BA%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24('.post-views-count').each(function()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24(this).text(response.data.new_count)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object mce-object-script" width="20" height="20" alt="&lt;script&gt;" />
<?php
}
}
add_action('wp_footer', 'ajax_post_views_counter');

/* ===== 后台管理功能 ===== */

/**
* 添加浏览量控制元框
*/
function add_post_views_meta_box() {
add_meta_box(
'post_views_control',
'📊 浏览量控制',
'post_views_meta_box_callback',
'post',
'side',
'high'
);
}
add_action('add_meta_boxes', 'add_post_views_meta_box');

/**
* 元框回调函数
*/
function post_views_meta_box_callback($post) {
wp_nonce_field('post_views_nonce', 'post_views_nonce_field');
$current_views = getPostViews($post->ID);
?>
<div style="margin-bottom: 15px;">
<label for="post_views_count" style="display: block; margin-bottom: 5px; font-weight: bold;">
当前浏览量:
</label>
<input type="number" id="post_views_count" name="post_views_count"
value="<?php echo esc_attr($current_views); ?>" style="width: 100%; padding: 8px;" min="0" />
</div>

<div style="background: #f7f7f7; padding: 10px; border-radius: 4px;">
<p style="margin: 0; font-size: 12px; color: #666;">
<strong>系统状态:</strong><br>
<?php if (function_exists('pvc_get_post_views')): ?>
✅ Post Views Counter 插件已激活
<?php else: ?>
ℹ️ 使用内置统计系统
<?php endif; ?>
</p>
</div>
<?php
}

/**
* 保存手动设置的浏览量
*/
function save_post_views_meta_box($post_id) {
// 安全检查
if (!isset($_POST['post_views_nonce_field']) ||
!wp_verify_nonce($_POST['post_views_nonce_field'], 'post_views_nonce')) {
return;
}

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!current_user_can('edit_post', $post_id)) return;

if (isset($_POST['post_views_count'])) {
manualSetPostViews($post_id, intval($_POST['post_views_count']));
}
}
add_action('save_post', 'save_post_views_meta_box');

/* ===== 实用工具函数 ===== */

/**
* 显示浏览量的快捷函数
* @param bool $format 是否格式化数字(添加千位分隔符)
*/
function the_post_views($format = true) {
$views = getPostViews(get_the_ID());
if ($format) {
echo number_format(intval($views));
} else {
echo $views;
}
}

/**
* 重置文章浏览量
*/
function reset_post_views($post_id) {
manualSetPostViews($post_id, 0);
}

/**
* 获取最受欢迎文章
* @param int $limit 文章数量
* @return array 文章数组
*/
function get_popular_posts($limit = 5) {
$args = array(
'post_type' => 'post',
'posts_per_page' => $limit,
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'views',
'value' => '0',
'compare' => '>'
)
)
);

return get_posts($args);
}

/* ===== 文章列表页增强 ===== */

/**
* 在文章列表页添加浏览量列
*/
function add_views_column($columns) {
$columns['post_views'] = '浏览量';
return $columns;
}
add_filter('manage_posts_columns', 'add_views_column');

/**
* 显示文章列表页的浏览量数据
*/
function display_views_column($column_name, $post_id) {
if ($column_name === 'post_views') {
echo number_format(intval(getPostViews($post_id)));
}
}
add_action('manage_posts_custom_column', 'display_views_column', 10, 2);

/**
* 让浏览量列可排序
*/
function views_column_sortable($columns) {
$columns['post_views'] = 'post_views';
return $columns;
}
add_filter('manage_edit-post_sortable_columns', 'views_column_sortable');
?>

模板调用方式

1. 基本显示(在 single.php、content.php 中)

1
2
3
4
5
6
7
8
9
<!-- 基本显示方式 -->
<span class="post-views-count"><?php the_post_views(); ?></span>

<!-- 带样式和图标 -->
<div class="post-meta-views">
<i class="icon-eye"></i>
<span class="post-views-count"><?php the_post_views(); ?></span> 次阅读
</div>

2. 热门文章小工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
$popular_posts = get_popular_posts(5);
if ($popular_posts) {
echo '<div class="popular-posts-widget">';
echo '<h3>热门文章</h3>';
echo '<ul>';
foreach ($popular_posts as $post) {
setup_postdata($post);
echo '<li>';
echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
echo ' <span>(' . number_format(getPostViews(get_the_ID())) . ')</span>';
echo '</li>';
}
wp_reset_postdata();
echo '</ul>';
echo '</div>';
}
?>

使用方式

Post Views Counter设置-显示-Admin Edit -开启