需要帮助发现错误在jQuery或php在wordpress


Need help spotting the mistake in either jQuery or php in wordpress

考虑到涉及到几种语言,我很难找出我的代码的哪一部分是错误的。

首先,我将将要运行的脚本排队,然后在插件主函数文件中使用wp_localize_script,如下所示:

// All scripts
add_action( 'wp_enqueue_scripts', 'theme_enqueue_scripts' );
function theme_enqueue_scripts() {
  // Enqueue and Localize AJAX JavaScript Functions File 
  wp_enqueue_script( 'ajax-categories-js', plugins_url( 'events-calendar-manager/inc/js/js.js' ), array('jquery'));
  wp_localize_script( 'ajax-categoreis-js', 'ajax_object_1', array( 'ajaxurl' => admin_url( 'admin_ajax.php' ) ) );

}
jQuery脚本如下所示:
// JavaScript Document
jQuery(document).ready(function($){
        console.log('Started Jquery');
        var year='', month='', category='';
     $('#next_link').click(function(e){
         e.preventDefault();
         var $aid =$('#next_link');
         year =$aid.data('year');
         month =$aid.data('month');
         category =$aid.data('category');
         console.log('category: ' + category);
     });
    $('#previous_link').click(function(e){
         e.preventDefault();
         var $dai =$('#previous_link');
         year =$dai.data('year');
         month =$dai.data('month');
         category =$dai.data('category');
         console.log('category: ' + category);
     });
    console.log('category: ' + category);

    $.ajax({
         cache: false,
         timeout: 8000,
         type: 'POST',
         data: {action: 'get_post_filter_by_date_and_category', year : year, month: month, category: category},
         url: ajax_object_1.ajaxurl,
         success: function(data) {},
         error: function() {}
    });
});

然而当我运行它时,我在控制台中得到一个错误:Uncaught ReferenceError: ajax_object_1 is not defined。这很奇怪,因为对象应该使用wp_localize_script函数传递。

是什么造成了这个问题?

这里有个打字错误

wp_enqueue_script( 'ajax-categories-js' ...
wp_localize_script( 'ajax-categoreis-js', ...

参见ajax-categories-js !== ajax-categoreis-js

wp_localize_script( 'ajax-categories-js', 'ajax_object_1', array( 'ajaxurl' => admin_url( 'admin_ajax.php' ) ) );

我对WP一无所知,但这里好像有个打字错误:

  wp_localize_script( 'ajax-categoreis-js', 'ajax_object_1', array( 'ajaxurl' => admin_url( 'admin_ajax.php' ) ) );

你可能不是想写...'ajax-categoreis-js'...,而是...'ajax-categories-js'...