我对ajax i WP有问题。该代码仅在我想显示所有帖子时有效,而当我尝试仅显示类别中的帖子时不起作用。
第一个循环显示正确的结果,但是在点击加载后,我有来自不同类别的帖子
我的 function.php
function miloszmich_my_load_more_scripts() {
wp_enqueue_script('jquery');
// register our main script but do not enqueue it yet
wp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/js/myloadmore.js', array('jquery') );
wp_localize_script( 'my_loadmore', 'miloszmich_loadmore_params', array(
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
'posts' => json_encode( $business_query->query_vars ), // everything about your loop is here
'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
'max_page' => 99
) );
wp_enqueue_script( 'my_loadmore' );
}
add_action( 'wp_enqueue_scripts', 'miloszmich_my_load_more_scripts' );
function miloszmich_loadmore_ajax_handler(){
// prepare our arguments for the query
$args = json_decode( stripslashes( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1;
$args['post_status'] = 'publish';
// 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( 'template-parts/content', get_post_format() );
endwhile;
endif;
die;
}
add_action('wp_ajax_loadmore', 'miloszmich_loadmore_ajax_handler');
add_action('wp_ajax_nopriv_loadmore', 'miloszmich_loadmore_ajax_handler');
我的 loadmore.js
jQuery(function($){
$('.miloszmich_my_load_more_scripts').click(function(){
event.preventDefault();
var button = $(this),
data = {
'action': 'loadmore',
'query': miloszmich_loadmore_params.posts, // that's how we get params from wp_localize_script() function
'page' : miloszmich_loadmore_params.current_page
};
$.ajax({
url : miloszmich_loadmore_params.ajaxurl, // AJAX handler
data : data,
type : 'POST',
beforeSend : function ( xhr ) {
button.text('Ładowanie...'); // change the button text, you can also add a preloader image
},
success : function( data ){
if( data ) {
button.text( 'Pokaż jeszcze więcej' ).prev().before(data); // insert new posts
miloszmich_loadmore_params.current_page++;
if ( miloszmich_loadmore_params.current_page == miloszmich_loadmore_params.max_page )
button.remove(); // if last page, remove the button
// you can also fire the "post-load" event here if you use a plugin that requires it
// $( document.body ).trigger( 'post-load' );
} else {
button.remove(); // if no data, remove the button as well
}
}
});
});
});
我的自定义页面上的循环
//define the inner query
$query_vars = [
'post_type' => 'post',
'posts_per_page' => 4,
'offset' => 1,
'cat' => 3,
'orderby' => array(
'date' =>'DESC',
'menu_order'=>'ASC',
)
];
// execute the inner query
$business_query = new WP_Query($query_vars);
while ( have_posts() ) : the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
// End the loop.
endwhile;
// if ( $wp_query->max_num_pages > 1 )
echo '<div class="col-md-6 col-md-offset-3"></div></row>'; // prevent for bad order
echo '<div class="btn miloszmich_my_load_more_scripts">Pokaż więcej wpisów</div>'; // load more fn
wp_reset_postdata();
所有类别的自定义页面上的步骤循环: 我有4条正确的信息,单击更多信息后,AJAX给我正确的4条信息。
具有1个类别循环的自定义页面上的步骤: 我有4个正确的帖子,单击更多的帖子后,AJAX给了我另外4个来自不同类别(随机)的帖子。
答案 0 :(得分:0)
如果我理解正确,则希望将查询限制在特定类别。 如果是这种情况,那么您需要在查询变量中包含category__not_in => array(YOUR_CAT_ID)