I have a drop down menu with taxonomy terms (thrid level terms), and when a term is selected I want to show its parent term through an ajax request. 但由于某种原因,我得到了“0”的值而不是父项值。有人可以帮助检查我的代码,看看我在这里缺少什么:
$("select#districts").change(function() {
$.ajax({
url: '/metrics/wp-admin/admin-ajax.php',
type: 'GET',
data: {action: 'county_request', term_id : $(this).children(":selected").prop("value")},
dataType: 'html',
success: function(data){
$('#district-county').html(data);
}
});
});
// 获取父项
function ajax_handle_request(){
$output = ajax_get_parent_term_by_id($_REQUEST['term_id']);
return $output;
}
add_action('wp_ajax_nopriv_county_request', 'ajax_handle_request');
add_action('wp_ajax_county_request', 'ajax_handle_request');
function ajax_get_parent_term_by_id($term_id){
$parent_term = get_term_by( 'id', $term_id->parent, 'state');
return $parent_term;
}