所以这是一个很长的问题,所以给你一个简单的例子,它将在步骤1)Cat1 cat2 Cat3中起作用。首先,你需要在表单中调用自定义分类法
步骤2)假设您有cat1 cat2作为复选框,其值为自定义分类id
步骤3)添加单击事件
步骤4)调用ajax函数
转到此示例-http://www.tinyjewelbox.com/product-category/jewelry/ - 我希望您想要实现这个点击复选框是自定义分类法,点击过滤根据分类法排序。
方法-让我们开始:-
这是输出模板页面\\u lat.php:
搜索结果(当前仅打印标题和帖子类型:
<?php
if($_GET[\'s\'] && !empty($_GET[\'s\'])){
$text =$_GET[\'s\'];
$posttype = $_GET[\'post_type\'];
}
else{
$posttype = \'custom_post_type\';
}
?>
//This will fetch your custom taxonomy cat1, cat2 , cat3
<form action="" method="" class="" id="filter-menu" >
<?php
$terms = get_terms(\'post_tag\',, array(\'hide_empty\' => false) );
foreach($terms as $filteroption)
{
<input type="checkbox" name="filter[]" value="<?php echo $filteroption->term_id; ?>" onclick="filtermenu()" >
<?php echo $filteroption->name; ?>
}
<input type="hidden" name="posttype" id="posttype" value="<?php echo $posttype; ?>" />
?>
<form>
<div class="container">
<div class="load_html">
<?php
$args = array(
\'post_per_page\'=> -1,
\'s\'=>$text
);
$query= new WP_Query($args);
while($query->have_posts()): $query->the_post();
?>
<div>
<h5><?php the_title(); ?></h5>
<strong>
<?php echo get_post_type(); ?>
</strong>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
单击时添加单击事件
<script> //this a click event of checkbox which call ajax action
function filtermenu(paged)
{
var filtercheckedbox = // get your checkbox value via jquery;
var posttype = // get your posttype value; jQuery(\'#posttype\').val(0);
jQuery.ajax({
url: AJAXURL,
data: {
\'action\' : \'filter_menu\' ,
\'checkbox\': filtercheckedbox,
\'posttype\' :posttype,
\'paged\' : 1,
},
type: \'POST\',
success: function(data) {
jQuery(".load_html").html(data);
}
});
}
</script>
将操作添加到函数中。php
<?php
// add this function to functions.php this is your ajax action
add_action( \'wp_ajax_filter_menu\', \'wp_ajax_filter_menu\' );
add_action( \'wp_ajax_nopriv_filter_menu\', \'wp_ajax_filter_menu\' );
function wp_ajax_filter_menu()
{
global $post, $wp_query;
$args = array(
\'post_type\' => \'$_POST[\'post_type\']\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'custom_taxonomy name\',
\'field\' => \'term_id\',
\'terms\' => array($_POST[\'checkbox\']),
),
),
\'orderby\' => \'ASC\',
);
$filteroption = new WP_Query( $args );
if ( $filteroption->have_posts() ) :
while ($filteroption->have_posts()) : $filteroption->the_post();
?>
<div>
<h5><?php the_title(); ?></h5>
</div>
<?php
endwhile;
else:
return false;
endif;
}
die;
}
?>