我需要实现按分类法过滤帖子的功能。当单击分类法时,只有那些拥有分类法数据的卡片才能保留下来。文章使用自定义Post类型的“扬声器”。有两个分类与术语“位置”和“国家”。我如何编写一个查询来过滤这些分类法?JS中的AJAX:
// === AJAX
$('.cat-list_item').on('click', function() {
$('.cat-list_item').removeClass('active');
$(this).addClass('active');
$.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
dataType: 'html',
data: {
action: 'filter_speakers',
category: $(this).data('slug'),
},
success: function(res) {
$('.speakers-list').html(res);
}
})
});
function.php:
// AJAX
function filter_speakers() {
}
add_action('wp_ajax_filter_speakers', 'filter_speakers');
add_action('wp_ajax_nopriv_filter_speakers', 'filter_speakers');
发布于 2022-08-17 11:34:40
在Ajax过滤器函数中:
$.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
dataType: 'html',
data: {
action: 'filter_projects',
category: $(this).data('slug'),
type: $(this).data('type'),
},
success: function(res) {
$('.project-tiles').html(res);
}
});PHP函数代码:
function filter_projects() {
$postType = $_POST['type'];
$catSlug = $_POST['category'];
$ajaxposts = new WP_Query([
'post_type' => $postType,
'posts_per_page' => -1,
'category_name' => $catSlug,
'orderby' => 'menu_order',
'order' => 'desc',
]);
$response = '';
if($ajaxposts->have_posts()) {
while($ajaxposts->have_posts()) : $ajaxposts->the_post();
$response .= get_template_part('templates/_components/project-list-item');
endwhile;
} else {
$response = 'empty';
}
echo $response;
exit;
}
add_action('wp_ajax_filter_projects', 'filter_projects');
add_action('wp_ajax_nopriv_filter_projects', 'filter_projects');https://stackoverflow.com/questions/73387657
复制相似问题