duanjing7651 2015-03-10 11:03 采纳率: 0%
浏览 58
已采纳

如何将wordpress类别作为数据发送到ajax? [关闭]

I want to send current category of the page to ajax. I am using WordPress for my blog website. I want to send current page category to infi.php, But I don't know how to do this.

my ajax

$.ajax({
        type: "POST",
        async: false,
        url: "/infi.php",
        data: {pcount:post_page_count},
        success:
        function(result){
            $("#gizinfi").append(result);
            }
      });
  • 写回答

1条回答 默认 最新

  • dongshu4221 2015-03-10 14:14
    关注

    To use AJAX properly in Wordpress there are a few steps you need to take.

    Firstly, assuming you are registering and enqueueing your javascript file properly (if you aren't or don't know what this means you should check out how to enqueue files in Wordpress), you need to localise the file. In your functions.php file you can localize a file like so...

    $data_array = array(
        'ajaxurl' => admin_url( 'admin-ajax.php' )
    );
    
    wp_register_script( 'YourAjaxScript', get_template_directory_uri() . 'js/example.js', array('jquery') );
    wp_localize_script( 'YourAjaxScript', 'myAjax', $data_array );
    

    Now you need some way of accessing the category ID from your javascript. You could simply include an empty span in your template somewhere and store your category_id as a data attribute, then you can find it easily using javascript. You can also add a 'nonce' for security reasons, this allows you to check that it is your ajax call that is accessing your PHP, not a randomer. So we'll add this to your header.php...

    <?php
    //For the sake of this we'll only get the first category
    $categories = get_the_category();
    $cat = ( !empty( $categories ) ? $categories[0]->term_id : false );
    
    //We'll also create a nonce for security
    $nonce = wp_create_nonce( 'ajax_nonce' );
    ?>
    
    <span id="category-id" data-category="<?php echo $cat; ?>" data-nonce="<?php echo $nonce; ?>"></span>
    

    Now in your example.js file you can create your AJAX function...

    $( document ).ready( function() {
    
        //Fetch your data variables
        var $cat = $( '#category-id' ).data('category');
        var $nonce = $( '#category-id' ).data('nonce');
    
        $.ajax({
            type: 'POST',
            url: myAjax.ajaxurl,
            data: {
                action: 'my_ajax_handler', //PHP function to handle AJAX request
                category: cat,
                nonce: $nonce
            },
            success: function( data ) {
                $("#gizinfi").append( data );
            }
        });
    
    });
    

    Then you need to create a PHP function that handles your AJAX request (could go in your infi.php file as long as you are including that file properly but may be better in your functions.php file). for example...

    /**
     * my_ajax_handler - handles my ajax response and returns some data
     * @return string - my data
     */
    function my_ajax_handler() {
        //First we'll validate the nonce and exit if incorrect
        if ( !wp_verify_nonce( $_POST['nonce'], 'ajax_nonce' ) ) { exit; }
    
        //Here we handle your ajax request and return whatever
        //All our data variables are saved in the $_POST array
        $category = $_POST['category'];
        return $category;
    }
    
    add_action("wp_ajax_my_ajax_handler", "my_ajax_handler");
    add_action("wp_ajax_nopriv_my_ajax_handler", "my_ajax_handler");
    

    Those last 2 lines bind the function to your ajax call. And that should be all you need.

    Hope that helps

    Dan

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献