我有两个不同的AJAX
在我的网站中加载更多功能。一个用于我的归档页面,第二个用于分类页面。
问题是,当两者都AJAX
处理程序函数位于“我的函数”中。php。当只有一个处理程序函数时,后者可以正常工作。
两种AJAX操作的JS代码:
/*
* AJAX Load More for archive page
*/
$(\'.load-more-archive\').click(function(){
var button = $(this),
data = {
\'action\': handler,
\'query\': posts_myajax,
\'page\' : current_page_myajax
};
$.ajax({
url : \'../wp-admin/admin-ajax.php\', // AJAX handler
data : data,
type : \'POST\',
beforeSend : function ( xhr ) {
button.hide();
},
success : function( data ){
if( data ) {
$(\'.wrapper-archive\').append(data);
button.show();
current_page_myajax++;
if ( current_page_myajax == max_page_myajax )
button.remove();
} else {
//button.remove();
}
}
});
});
/*
* AJAX Load More for taxonomy page
*/
$(\'.load-more-tax\').click(function(){
var button = $(this),
data = {
\'action\': handler,
\'tax_post_type\': current_post_type,
\'tax_term\': tax_term_id,
\'query\': posts_myajax,
\'page\' : current_page_myajax,
};
$.ajax({
url : \'../wp-admin/admin-ajax.php\',
data : data,
type : \'POST\',
beforeSend : function ( xhr ) {
button.hide();
},
success : function( data ){
if( data ) {
$(\'.wrapper-taxonomy\').append(data);
button.show();
current_page_myajax++;
if ( current_page_myajax == max_page_myajax )
button.remove();
} else {
//button.remove();
}
}
});
});
下面是两个AJAX处理程序的PHP代码:
/**
* AJAX handler for archive page
*/
function archive_loadmore_ajax_handler(){
$args = json_decode( stripslashes( $_POST[\'query\'] ), true );
$args[\'paged\'] = $_POST[\'page\'] + 1;
$args[\'post_type\'] = $_POST[\'action\'];
$args[\'posts_per_page\'] = 4;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( \'template-parts/content\', \'cpt-archive\' );
endwhile;
endif;
die;
}
add_action(\'wp_ajax_\'. $_POST[\'action\'] .\'\', \'archive_loadmore_ajax_handler\');
add_action(\'wp_ajax_nopriv_\'. $_POST[\'action\'] .\'\', \'archive_loadmore_ajax_handler\');
/**
* AJAX handler for taxonomy page
*/
function taxonomy_loadmore_ajax_handler(){
$args = json_decode( stripslashes( $_POST[\'query\'] ), true );
$args[\'paged\'] = $_POST[\'page\'] + 1;
$args[\'post_type\'] = $_POST[\'tax_post_type\'];
$args[\'posts_per_page\'] = 4;
$args[\'tax_query\'] = array(
array (
\'taxonomy\' => $_POST[\'action\'],
\'field\' => \'id\',
\'terms\' => $_POST[\'tax_term\']
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( \'template-parts/content\', \'cpt-archive\' );
endwhile;
endif;
die;
}
add_action(\'wp_ajax_\'. $_POST[\'action\'] .\'\', \'taxonomy_loadmore_ajax_handler\');
add_action(\'wp_ajax_nopriv_\'. $_POST[\'action\'] .\'\', \'taxonomy_loadmore_ajax_handler\');
我知道在致电
AJAX
脚本,但我找不到解决它的方法。任何帮助都将不胜感激。非常感谢。
EDIT
我按照以下建议更新了PHP AJAX和JS AJAX:
JS
$(\'.load-more-archive\').click(function(){
var button = $(this),
data = {
\'action\': \'load_archive\',
\'my_post_type\': current_post_type,
\'query\': posts_myajax,
\'page\' : current_page_myajax
};
$.ajax({
url : \'http://localhost:8888/sites/mysite/wp-admin/admin-ajax.php\',
data : data,
type : \'POST\',
beforeSend : function ( xhr ) {
button.hide();
},
success : function( data ){
if( data ) {
$(\'.wrapper-archive\').append(data);
isLoaded();
button.show(); // Insert new posts
current_page_myajax++;
if ( current_page_myajax == max_page_myajax )
button.remove();
} else {
//button.remove();
}
}
});
});
PHP
function mysite_archive_loadmore_ajax_handler(){
$args = json_decode( stripslashes( $_POST[\'query\'] ), true );
$args[\'paged\'] = $_POST[\'page\'] + 1;
$args[\'post_type\'] = $_POST[\'my_post_type\'];
$args[\'posts_per_page\'] = 4;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( \'template-parts/content\', \'cpt-archive\' );
endwhile;
endif;
die;
}
add_action(\'wp_ajax_load_archive\', \'mysite_archive_loadmore_ajax_handler\');
add_action(\'wp_ajax_nopriv_load_archive\', \'mysite_archive_loadmore_ajax_handler\');