所以我在网上找到的教程之后,使用ajax创建并加载更多帖子。刚开始,只有一个按钮时它就工作了,所以我创建了另一个按钮,第一次尝试时它工作了,但当我再次尝试时,它就停止了工作。我只是想随机编辑ajax或与之相关的东西,然后我会删除缓存并再次刷新,有时会正常,有时不会。我真的不知道到底发生了什么,因为我对ajax、javascript和jquery还是新手。
总之,这里是我的ajax:
jQuery(function($){
$(\'.loadmore\').click(function(){
var button = $(this),
data = {
\'action\': \'loadmore\',
\'query\': kamisaha_loadmore_param.posts,
\'page\' : kamisaha_loadmore_param.current_page
};
$.ajax({
url : kamisaha_loadmore_param.ajaxurl,
data : data,
type : \'POST\',
beforeSend : function ( xhr ) {
button.text(\'Loading...\');
},
success : function( data ){
if( data ) {
button.html( \'Tampilkan Lagi <i class="fas fa-caret-down"></i>\' ).prev().after(data);
kamisaha_loadmore_param.current_page++;
if ( kamisaha_loadmore_param.current_page == kamisaha_loadmore_param.max_page )
button.remove();
} else {
button.remove();
}
}
});
});
});
jQuery(function($){
$(\'.movie\').click(function(){
var button = $(this),
data = {
\'action\': \'loadmore_movie\',
\'query\': posts_movie,
\'page\' : current_page_movie
};
$.ajax({
url : kamisaha_loadmore_param.ajaxurl,
data : data,
type : \'POST\',
beforeSend : function ( xhr ) {
button.text(\'Loading SDMASKLDJ ASLDJ ASLKJ...\');
},
success : function( data ){
if( data ) {
button.html( \'Tampilkan Lagi <i class="fas fa-caret-down"></i>\' ).prev().after(data);
current_page_movie++;
if ( current_page_movie == max_page_movie )
button.remove();
} else {
button.remove();
}
}
});
});
});
如果我的代码看起来很糟糕,我很抱歉,因为我只是公然把它包装在一起。非常感谢您的帮助,谢谢。
编辑:这是我的功能。php
function load_more_script(){
if(!is_home() && !is_front_page()){
global $wp_query;
wp_register_script( \'kamisaha_loadmore\', get_stylesheet_directory_uri() . \'/assets/js/loadmore.js\', array(\'jquery\') );
wp_localize_script( \'kamisaha_loadmore\', \'kamisaha_loadmore_param\', array(
\'ajaxurl\' => admin_url(\'admin-ajax.php\'), // WordPress AJAX
\'posts\' => json_encode( $wp_query->query_vars ),
\'current_page\' => get_query_var( \'paged\' ) ? get_query_var(\'paged\') : 1,
\'max_page\' => $wp_query->max_num_pages
) );
wp_enqueue_script( \'kamisaha_loadmore\' );
}
}
add_action( \'wp_enqueue_scripts\', \'load_more_script\' );
function misha_loadmore_ajax_handler(){
// prepare our arguments for the query
$args = json_decode( stripslashes( $_POST[\'query\'] ), true );
$args[\'paged\'] = $_POST[\'page\'] + 1; // we need next page to be loaded
$args[\'post_type\'] = \'anime\';
// it is always better to use WP_Query but not here
query_posts( $args );
if( have_posts() ) :
// run the loop
while( have_posts() ): the_post();
get_template_part(\'content\', \'list\');
endwhile;
endif;
die; // here we exit the script and even no wp_reset_query() required!
}
add_action(\'wp_ajax_loadmore\', \'misha_loadmore_ajax_handler\'); // wp_ajax_{action}
add_action(\'wp_ajax_nopriv_loadmore\', \'misha_loadmore_ajax_handler\'); // wp_ajax_nopriv_{action}
function loadmore_movie(){
// prepare our arguments for the query
$args = json_decode( stripslashes( $_POST[\'query\'] ), true );
$args[\'paged\'] = $_POST[\'page\'] + 1; // we need next page to be loaded
$args[\'post_type\'] = \'anime\';
$args[\'meta_key\'] = \'ks_type\';
$args[\'meta_value\'] = \'Movie\';
$args[\'orderby\'] = \'modified\';
$args[\'order\'] = \'DESC\';
// it is always better to use WP_Query but not here
query_posts( $args );
if( have_posts() ) :
// run the loop
while( have_posts() ): the_post();
get_template_part(\'content\', \'list\');
endwhile;
endif;
die; // here we exit the script and even no wp_reset_query() required!
}
add_action(\'wp_ajax_loadmore_movie\', \'loadmore_movie\'); // wp_ajax_{action}
add_action(\'wp_ajax_nopriv_loadmore_movie\', \'loadmore_movie\'); // wp_ajax_nopriv_{action}
这是我的第一个循环(ajax总是有效的):
if (have_posts()) : while(have_posts()) : the_post();
<get_template_part(\'content\', \'list\');
endwhile; else : echo "No results found"; endif; wp_reset_postdata();
if ( $wp_query->max_num_pages > 1 )
echo \'<div class="loadmore box">Tampilkan Lagi <i class="fas fa-caret-down"></i></div>\';
这是我的第二个循环(Ajax有时不起作用,但在第一次尝试时就起作用了):
$args = array(\'post_type\' => \'anime\', \'meta_key\' => \'ks_type\', \'meta_value\' => \'Movie\', \'order\' => $set_order, \'orderby\' => $set_orderby, \'meta_query\' => array(\'rating\' => array(\'key\' => \'ks_score\', \'type\' => \'NUMERIC\'))); $q = NEW WP_Query($args); if ($q->have_posts()) : while($q->have_posts()) : $q->the_post();
get_template_part(\'content\', \'list\');
endwhile; else : echo "No results found"; endif; wp_reset_postdata();
if ( $q->max_num_pages > 1 )
echo \'<div class="loadmore movie box">Tampilkan Lagi <i class="fas fa-caret-down"></i></div>\';
<script>
var posts_movie = \'<?php echo serialize( $q->query_vars ) ?>\',
current_page_movie = 1,
max_page_movie = <?php echo $q->max_num_pages ?>
</script>