Javascript 如何在WordPress中实现自定义逻辑?

Javascript 如何在WordPress中实现自定义逻辑?,javascript,php,jquery,wordpress,Javascript,Php,Jquery,Wordpress,我正在我的网站上工作,我想补充两件事 一个链接,当用户点击它时,他/她的用户id和当前的帖子id就会被插入到数据库中,并且计数器会增加 假设用户登录并打开帖子,链接如下:我感兴趣(0)这里0是点击此链接的唯一登录用户总数的计数值。第二个链接将是这样的:我想工作(0)同样的逻辑适用于这里** 最后需要显示特定职位(自定义职位类型:job)的感兴趣人员和工作人员的数量 有一个表,其中每一行都有帖子(作业)的标题。我可以通过WordPress本机函数获取帖子的值,然后将其与数据库中的帖子id进行比

我正在我的网站上工作,我想补充两件事

  • 一个链接,当用户点击它时,他/她的用户id和当前的帖子id就会被插入到数据库中,并且计数器会增加

  • 假设用户登录并打开帖子,链接如下:我感兴趣(0)这里0是点击此链接的唯一登录用户总数的计数值。第二个链接将是这样的:我想工作(0)同样的逻辑适用于这里**

最后需要显示特定职位(自定义职位类型:job)的感兴趣人员和工作人员的数量

有一个表,其中每一行都有帖子(作业)的标题。我可以通过WordPress本机函数获取帖子的值,然后将其与数据库中的帖子id进行比较,然后检索其状态(noofinterested和noofworking),然后使用jQuery通过AJAX进行注入

我已经为此创建了表,但我遇到了困难:

  • 通过jquery获取帖子ID和用户ID由于某些原因,我无法修改当前的自定义帖子模板,因为我正在使用wp job manager进行自定义帖子
  • 我所需要的是找到一种方法,让独特的人依靠特定的职位
  • 到目前为止,我想到了一种方法,通过jQuery将两个链接和add click函数附加到通过jQuery的链接中,并在其中添加ajax以获取数据和输入到我编写的PHP文件中,在该PHP文件中,它将通过post值插入表中,然后返回响应。在第二页中,我想在表行中插入状态,我将使用jQuery来实现这一点

    这是我目前手头上的

    PHP

    
    
    jQuery

     jQuery("#link").click(function(){
        if(jQuery("body").hasClass("single")){
        jQuery('<p id="stauts-interested">interested 24</p><br><p id="status-work">Working 43</p>').appendTo(".job-manager-single-alert-link");
        }
    });
    
    jQuery(#link”)。单击(函数(){
    if(jQuery(“body”).hasClass(“single”)){
    jQuery(“

    感兴趣的24个”


    正在工作的43个”

    ).appendTo(“.job manager single alert link”); } });

    现在,感兴趣的和正在工作的用户的数量应该从数据库中获取,并且对于单击的唯一登录用户也应该增加。

    为什么不在自定义帖子类型中添加一个自定义字段,而不是使用自定义表呢?这几乎是一个最好的例子,说明什么时候使用它是合适的。只需使用WP Ajax钩子上的
    get_post_meta
    update_post_meta
    将当前用户id添加到一个数组中

    add_action( 'wp_ajax_increment_cpt_interests' );
    add_action( 'wp_ajax_nopriv_increment_cpt_interests' );
    
    function increment_cpt_interests(){
        extract( $_POST );
    
        // Get current interested people
        $interested_people = get_post_meta( $post_id, 'interested', true );
        $interested_user   = get_current_user_id();
    
        // If current user isn't already "interested"
        if( !in_array( $interested_user, $interested_people ){
            // Add them to the array of interested people
            update_post_meta( $post_id, 'interested_people', $interested_people[$interested_user] );
        }
    }
    

    只需使用(阅读更多内容)将post ID、ajaxurl(以及当前用户ID)传递给您想要单击的按钮即可

    为什么不在自定义帖子类型中添加自定义字段,而不是使用自定义表?这几乎是一个最好的例子,说明什么时候使用它是合适的。只需使用WP Ajax钩子上的
    get_post_meta
    update_post_meta
    将当前用户id添加到一个数组中

    add_action( 'wp_ajax_increment_cpt_interests' );
    add_action( 'wp_ajax_nopriv_increment_cpt_interests' );
    
    function increment_cpt_interests(){
        extract( $_POST );
    
        // Get current interested people
        $interested_people = get_post_meta( $post_id, 'interested', true );
        $interested_user   = get_current_user_id();
    
        // If current user isn't already "interested"
        if( !in_array( $interested_user, $interested_people ){
            // Add them to the array of interested people
            update_post_meta( $post_id, 'interested_people', $interested_people[$interested_user] );
        }
    }
    

    只需使用(阅读更多内容)将post ID、ajaxurl(以及当前用户ID)传递给您想要单击的按钮即可

    你能再解释一下吗?你能再解释一下吗?