1
我正在尝试使用单选按钮制作Ajax后置过滤器。以便用户可以使用单选按钮筛选多个类别。WordPress中的单选按钮的Ajax后置过滤器
这是我所使用的代码:
前端形式:
<form id="filter">
<?php
if($terms = get_terms('category', 'orderby=name')) : // to make it simple I use default categories
foreach ($terms as $term) :
echo '<input type="checkbox" name="category[]" value="' . $term->term_id . '" class="br">' . $term->name;
echo '';
endforeach;
endif;
?>
<div class="filter-output"></div>
</form>
的script.js:
jQuery(document).ready(function($){
$('#filter .br').click(function(){
// Declaratie van array
var choices = {};
$('.contents').remove();
$('.filter-output').empty();
$('input[type=checkbox]:checked').each(function() {
if (!choices.hasOwnProperty(this.name))
choices[this.name] = [this.value];
else
choices[this.name].push(this.value);
});
console.log(choices);
$.ajax({
url: ajaxobject.ajaxurl,
type :'POST',
data : {
'action' : 'call_post', // Naam van de PHP functie
'choices' : choices,
},
success: function (result) {
$('.filter-output').append(result);
// Voor testen - Resultaat (Kan later verwijderd worden)
//console.log(Resultaat);
//console.log(Keuzes);
},
error: function(err){
// Voor testen - Error (Kan later verwijderd worden)
console.log(err);
console.log(choices);
}
});
})
});
的functions.php:
add_action('wp_ajax_call_post', 'call_post');
add_action('wp_ajax_nopriv_call_post', 'call_post');
function call_post(){
// Verkijgen van AJAX data:
$choices = $_POST['choices'];
$meta_query = array('relation' => 'OR');
foreach($choices as $Key=>$Value){
if(count($Value)){
foreach ($Value as $Inkey => $Invalue) {
$meta_query[] = array('key' => $Key, 'value' => $Invalue, 'compare' => '=');
}
}
}
$args = array(
'post_type' => 'post',
'meta_query' =>$meta_query
);
$query = new WP_Query($args);
//if(! empty ($params['template'])) {
////$template = $params['template'];
if($query->have_posts()) :
while($query->have_posts()): $query->the_post();
get_template_part('content');
endwhile;
wp_reset_query();
else :
wp_send_json($query->posts);
endif;
//}
die(); }
但我不断收到以下错误:
script.js:20 Uncaught ReferenceError: ajaxobject is not defined
而且我不知道如何解决它。所以我希望有人能帮助我。我对AJAX的经验很少,这就是为什么我要求帮助。
链接到网站: http://5.79.72.162/~delta/category/vacancies/
谢谢:),它需要连接到admin-ajax.php。现在只需要找出为什么在制作阵列时不显示信息。 – Rachelle