编码的字preSS阿贾克斯点击计数器 [英] coding ajax click counter on wordpress

查看:146
本文介绍了编码的字preSS阿贾克斯点击计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助的编码字preSS的AJAX点击计数器.... 我已经尝试过了几天,似乎无法推测出来。 我有一个模板页面,列出了自定义后类型和显示混音带/专辑 和所有的人都可以下载。模板页面是codeD在PHP这样的链接是一个变量,而不是静态的html。我需要创建一个计数器计数,并显示有多少次每个下载链接被点击....所以一个点击计数器....

i need help coding an ajax click counter for wordpress.... i have tried for a few days and can't seem to figure it out. i have a template page that lists a custom post type and displays mixtapes/albums and all of them are downloadable. the template page is coded in php so the links are a variable and not static html. i need to create a counter that counts and displays how many times each download link is clicked.... so a click counter....

这是在code模板页面上的链接。

this is the code for a link on the template page.

<a href="<?php echo get_post_meta($post->ID,'mixtape_link',true); ?>" class="button small"><span>Download Now!</span></a>

我已经尝试吨脚本和插件,并不能似乎找到答案。

i have tried tons of scripts and plugins and cant seem to figure it out.

如果有人可以张贴的例子code或做到这一点对我来说将是极大的pciated AP $ P $

if someone could post an example code or do this for me it would be greatly appreciated

推荐答案

HTML:

<a href="<?php echo get_post_meta($post->ID,'mixtape_link',true); ?>" id="thisdownloads" class="button small"><span>Download Now!</span></a>

<script>
    jQuery(document)ready(function() {

      jQuery(this).on('change', '#thisdownloads', function() {

        jQuery.ajax({
        type: "POST",
        url: "/wp-admin/admin-ajax.php",

        data: {
            action: 'update_downloads',
            ID: '<?php get_the_ID(); ?>'// if within the loop you need a correct id for this to work...........
        },
        success: function (output) {}
           console.log(output);
        });


      });
    });

</script>

PHP的:

php:

    add_action('wp_ajax_update_downloads', 'update_downloads');
add_action('wp_ajax_nopriv_update_downloads', 'update_downloads');

function update_downloads() {

  $id= sanitize_text_fields($_POST['ID']);

  $downloads= get_post_meta($id,'_downloads',true);

  if(!$downloads):
     $downloads=1;
  else:
     $downloads++;
  endif;

  update_post_meta($id,'_downloads', $downloads);
  echo $downloads;
  exit();

}

应该是足够接近,如果你不能做这项工作,你需要研究以下内容:

should be close enough, if you cant make this work you need to research the following:

  1. 在jQuery的上的变化
    2.jquery安全模式(字preSS)
  2. 在jQuery的AJAX字preSS
  3. 字preSS AJAX功能添加行为
  4. update_post_meta
  1. jquery "on" change
    2.jquery safe mode (wordpress)
  2. jquery ajax wordpress
  3. wordpress ajax functions add action
  4. update_post_meta

这篇关于编码的字preSS阿贾克斯点击计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆