我将其用于城市/州选择。“sehir”是自定义分类名称。
头文件.php
<script type="text/javascript">
$(function(){
$('#main_scat').change(function(){
var $mainsCat=$('#main_scat').val();
// call ajax
$("#sub_scat").empty();
$.ajax({
url:"<?php bloginfo('url'); ?>/wp-admin/admin-ajax.php",
type:'POST',
data:'action=my_special_ajax_calls&main_catids=' + $mainsCat,
success:function(results)
{
// alert(results);
$("#sub_scat").removeAttr("disabled");
$("#sub_scat").append(results);
}
});
});
});
</script>
函数.php
function implement_ajax_sehir() {
if(isset($_POST['main_catids'])) {
if($_POST['main_catids'] != 0) {
$categories= get_categories('child_of='.$_POST['main_catids'].'&hide_empty=1&taxonomy=sehir');
foreach ($categories as $cat) {
$option .= '<option value="'.$cat->term_id.'">';
$option .= $cat->cat_name;
$option .= '</option>';
}
echo '<option value="0" selected="selected">İlçe Seçiniz</option>'.$option;
die();
} else {
echo '<option value="0" selected="selected">İlçe Seçiniz</option>';
}
}
}
add_action('wp_ajax_my_special_ajax_calls', 'implement_ajax_sehir');
add_action('wp_ajax_nopriv_my_special_ajax_calls', 'implement_ajax_sehir');
主要类别选择框代码:
<?php
$args = array(
'taxonomy' => 'sehir',
'name'=>'main_scat',
'hide_empty'=>1,
'depth'=>1,
'hierarchical'=> 1,
'show_count' => 0,
'show_option_all'=>'Şehir Seçiniz'
);
wp_dropdown_categories( $args );
?>
子类别选择框:
<select name="sub_scat" id="sub_scat" disabled="disabled"><option value="0" selected="selected">İlçe Seçiniz</option></select>
您可以通过这种方式进行改进。