我遵循了here的教程。但我想让它用ajax加载来过滤多类别/分类法。
设置过滤器后,当我点击加载更多时,它会显示所有类别帖子,而不是已检查的类别
无论如何都要在加载更多时显示已检查的类别。
的index.php
<form id="misha_filters" action="#">
<div class="checkbox-wrap">
<input type="checkbox" id="fi1-all" name="categoryfilter-all" value="all" checked>
<label for="fi1-all" class="cat-all">
<?php
$args = array(
'posts_per_page' => -1,
'post_status' => 'publish',
'category' => '-1', // except uncategorized
);
$posts_array = get_posts( $args );
$total_post = count($posts_array);
?>
All (<?php echo $total_post ?>)
<span class="icon-check"><?php include 'inc/vectors/check-for-checkbox-white.svg' ?></span>
</label>
</div>
<?php
if( $terms = get_terms( 'category', 'orderby=name' ) ) :
$a = 0;
foreach ( $terms as $term ) :
$a++;
if ( $term->slug !== 'uncategorized' ) :
?>
<div class="checkbox-wrap">
<input type="checkbox" id="fi1-<?php echo $a ?>" name="categoryfilter-<?php echo $a ?>" value="<?php echo $term->term_id ?>">
<label for="fi1-<?php echo $a ?>" class="cat-<?php echo $term->slug ?>">
<?php echo $term->name ?> (<?php echo $term->count; ?>)
<span class="icon-check"><?php include 'inc/vectors/check-for-checkbox-white.svg' ?></span>
</label>
</div>
<?php
endif;
endforeach;
endif;
?>
function.php
add_action( 'wp_enqueue_scripts', 'misha_script_and_styles');
function misha_script_and_styles() {
global $wp_query;
wp_register_script( 'misha_scripts', get_stylesheet_directory_uri() . '/inc/script-filter-loadmore.js', array('jquery') );
wp_localize_script( 'misha_scripts', 'misha_loadmore_params', array(
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php',
'posts' => json_encode( $wp_query->query_vars ),
'current_page' => $wp_query->query_vars['paged'] ? $wp_query->query_vars['paged'] : 1,
'max_page' => $wp_query->max_num_pages
) );
wp_enqueue_script( 'misha_scripts' );
}
add_action('wp_ajax_loadmorebutton', 'misha_loadmore_ajax_handler');
add_action('wp_ajax_nopriv_loadmorebutton', 'misha_loadmore_ajax_handler');
function misha_loadmore_ajax_handler(){
$taxonomy = 'category';
$taxonomy_terms = get_terms( $taxonomy, array(
'hide_empty' => 0,
'fields' => 'ids',
'exclude' => get_cat_ID('uncategorized'),
));
$form_filter_checkboxes = array($_POST['categoryfilter-1'], $_POST['categoryfilter-2'],
$_POST['categoryfilter-3'], $_POST['categoryfilter-4'],
$_POST['categoryfilter-5'], $_POST['categoryfilter-6'],
$_POST['categoryfilter-7']);
$params = json_decode( stripslashes( $_POST['query'] ), true );
$params['paged'] = $_POST['page'] + 1;
$params['post_status'] = 'publish';
$params['posts_per_page'] = '3';
$params['tax_query'] = array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $form_filter_checkboxes,
)
;
query_posts( $params );
global $wp_query;
if( have_posts() ) :
while( have_posts() ): the_post();
get_template_part( 'template-parts/post/post-type-content', get_post_format() );
endwhile;
endif;
die;
}
add_action('wp_ajax_mishafilter', 'misha_filter_function');
add_action('wp_ajax_nopriv_mishafilter', 'misha_filter_function');
function misha_filter_function(){
$params = array(
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 2
);
$taxonomy = 'category';
$taxonomy_terms = get_terms( $taxonomy, array(
'hide_empty' => 0,
'fields' => 'ids',
'exclude' => get_cat_ID('uncategorized'),
));
$form_filter_checkboxes = array($_POST['categoryfilter-1'], $_POST['categoryfilter-2'],
$_POST['categoryfilter-3'], $_POST['categoryfilter-4'],
$_POST['categoryfilter-5'], $_POST['categoryfilter-6'],
$_POST['categoryfilter-7']);
if ( $_POST['categoryfilter-all'] === 'all') :
$params['tax_query'] = array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $taxonomy_terms,
)
);
else :
$params['tax_query'] = array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $form_filter_checkboxes,
)
);
endif;
query_posts( $params );
global $wp_query;
if( have_posts() ) :
ob_start();
while( have_posts() ): the_post();
echo $_POST['categoryfilter-all'];
get_template_part( 'template-parts/post/post-type-content', get_post_format() );
endwhile;
$posts_html = ob_get_contents();
ob_end_clean();
else:
$posts_html = '<p>Nothing found for your criteria.</p>';
endif;
echo json_encode( array(
'posts' => serialize( $wp_query->query_vars ),
'max_page' => $wp_query->max_num_pages,
'found_posts' => $wp_query->found_posts,
'content' => $posts_html
) );
die();
}
JS档案
jQuery(function($){
/*
* Load More
*/
$('#misha_loadmore').click(function(){
$.ajax({
url : misha_loadmore_params.ajaxurl, // AJAX handler
data : {
'action': 'loadmorebutton', // the parameter for admin-ajax.php
'query': misha_loadmore_params.posts, // loop parameters passed by wp_localize_script()
'page' : misha_loadmore_params.current_page // current page
},
type : 'POST',
beforeSend : function ( xhr ) {
$('#misha_loadmore').text('Loading...'); // some type of preloader
},
success : function( posts ){
if( posts ) {
$('#misha_loadmore').text( 'More posts' );
$('#misha_posts_wrap').append( posts ); // insert new posts
misha_loadmore_params.current_page++;
if ( misha_loadmore_params.current_page == misha_loadmore_params.max_page )
$('#misha_loadmore').hide(); // if last page, HIDE the button
} else {
$('#misha_loadmore').hide(); // if no data, HIDE the button as well
}
}
});
return false;
});
/*
* Filter
*/
$('#misha_filters').submit(function(){
$.ajax({
url : misha_loadmore_params.ajaxurl,
data : $('#misha_filters').serialize(), // form data
dataType : 'json', // this data type allows us to receive objects from the server
type : 'POST',
beforeSend : function(xhr){
$('#misha_filters').find('button').text('Filtering...');
},
success : function( data ){
// when filter applied:
// set the current page to 1
misha_loadmore_params.current_page = 1;
// set the new query parameters
misha_loadmore_params.posts = data.posts;
// set the new max page parameter
misha_loadmore_params.max_page = data.max_page;
// change the button label back
$('#misha_filters').find('button').text('Apply filter');
// insert the posts to the container
$('#misha_posts_wrap').html(data.content);
// hide load more button, if there are not enough posts for the second page
if ( data.max_page < 2 ) {
$('#misha_loadmore').hide();
} else {
$('#misha_loadmore').show();
}
}
});
// do not submit the form
return false;
});
});
答案 0 :(得分:0)
LinkedList<Long> list = new LinkedList<>();
int pos=1;
System.out.println("pos+1:"+(pos+1));
list.remove((int)pos+1); //Add an explicit cast here to your above code!
以上行未调用过滤器查询循环。这就是为什么,你无法获得由misha_filter_function(){}设置的查询循环。
更新(已修复):请检查以下更新。
实际问题是
$params = json_decode( stripslashes( $_POST['query'] ), true );
返回的不是JSON格式数据,而是返回序列化数据形式。 所以使用json_encode没有任何意义。只需更改此行
即可$_POST['query']
到这个
$params = json_decode( stripslashes( $_POST['query'] ), true );
然后它必须工作。一切顺利(y)
答案 1 :(得分:0)
我不知道您是否还有问题,但这是我的解决方案,只需更改即可:
echo json_encode( array(
'posts' => serialize( $wp_query->query_vars ),
'max_page' => $wp_query->max_num_pages,
'found_posts' => $wp_query->found_posts,
'content' => $posts_html
) );
TO
echo json_encode( array(
'posts' => json_encode( $wp_query->query_vars ),
'max_page' => $wp_query->max_num_pages,
'found_posts' => $wp_query->found_posts,
'content' => $posts_html
) );
我刚刚将serialize
更改为json_encode
,这是正确的方法,您将获得您的条款帖子!
您不应像Deepak那样使用反序列化。这是非常不安全的。 manual中有一个红色警告,通过搜索PHP对象注入,您可以轻松找到其危险性的解释。
帮个忙,并使用json_encode / decode。因为query_vars是一个简单的数组,所以不会有什么区别。