编码的字preSS阿贾克斯点击计数器计数器、preSS、阿贾克斯

2023-09-10 22:10:13 作者:東風撫落塵

我需要帮助的编码字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:

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