【发布时间】:2022-01-01 04:13:12
【问题描述】:
使用 AJAX 单击按钮时,如何计算我的 WordPress 帖子的帖子浏览量?
目前,每当我刷新页面时,它都会计算帖子视图,我想用 ajax 调用 tha 函数。
请检查我当前用于显示帖子查看次数的代码
functions.php 内部
if ( ! function_exists( 'count_views' ) ) :
// Get the value of view
function count_views($postID) {
$count_key = 'wpb_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count ==''){
$count = 1;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '1');
} else {
$count++;
update_post_meta($postID, $count_key, $count);
}
}
endif;
然后在 single.php 上,我用 get_the_ID();
调用了函数 count_views<?php count_views(get_the_ID()); ?>
要检索我使用的观看次数:
<li>
<i class="fa fa-eye"></i>
<?php
if (get_post_meta(get_the_ID(), 'wpb_post_views_count', true) == '') {
echo '0';
} else {
echo get_post_meta(get_the_ID(), 'wpb_post_views_count', true);
};
?>
</li>
如何使用 javascript Ajax 调用来调用 count_views(get_the_ID()) 函数。
【问题讨论】:
标签: javascript php jquery ajax wordpress