ajax - 在 WordPress 上编写 AJAX 点击计数器
问题描述
我正在研究 WordPress 上的 ajax 点击计数器。当用户点击时,它会显示点击次数。我的代码:
html
<?php
$link_download = get_field('link_download');
?>
<a href="<?php echo $link_download ?>" id="taive" >DownLoad</a>
<span id="counter"></span>
计数器.js
(function($) {
$(document).ready(function() {
$('#taive').click(function() {
$.ajax({
type: "POST",
dataType: 'json',
url: "/wp-admin/admin-ajax.php",
data: {
action: 'set_download',
ID: '<?php get_the_ID(); ?>'
},
cache: false,
async: false,
success: function(response) {
if (response.error.message) {
alert(response.error.message);
} else if (response.success) {
jQuery('#count').html(response.data);
}
}
});
});
});
})
函数.php
function js_enqueue_scripts() {
wp_enqueue_script ("my-ajax-handle", get_stylesheet_directory_uri() . "/js/counter.js", array('jquery'));
wp_localize_script('my-ajax-handle', 'the_ajax_script', array('ajaxurl' =>admin_url('admin-ajax.php')));
}
add_action("wp_enqueue_scripts", "js_enqueue_scripts");
add_action('wp_ajax_set_download', 'set_download');
add_action('wp_ajax_nopriv_set_download', 'set_download');
function set_download() {
$downloads= get_post_meta($post->ID,'count',true);
if(!$downloads):
$downloads=1;
else:
$downloads++;
endif;
update_post_meta($post->ID,'count', $downloads);
echo $downloads;
exit();
};
我想在 id="counter" 上显示点击次数,我该怎么做?
解决方案
推荐阅读
- javascript - 如何在 IONIC 3 的 ion-tab 中停止传播
- javascript - 为什么我必须像“state.topicsReducer.topics”一样访问我的 Redux 状态?
- web-scraping - 通过设置多个过滤器来抓取画面仪表板
- react-native - 如何从 Promise 中获取号码
- python-3.x - 将字符串列表转换为 x,y 元组列表
- python - 用普通括号括起来的方括号有什么作用?
- javascript - 如何在引导下拉功能中正确显示所选值?
- flutter - ListView.build 过滤后渲染错误的元素
- ruby-on-rails - Rails ActiveStorage 为 PNG 上传添加黑色背景颜色
- c - time_base 适用于视频但对音频有不良影响