要根据用户在其他输入/选择中选择的内容更改选择,您需要使用WP AJAX API.
在您的功能中。php,使用wp_localize_script() 获取前端可用的AJAX url并定义AJAX操作回调
function mytheme_custom_scripts() {
if ( is_page(\'Support\') ) {
$scriptSrc = get_stylesheet_directory_uri() . \'/js/freshdeskdata.js\';
wp_enqueue_script( \'myhandle\', $scriptSrc , array(), \'1.0\', false );
wp_localize_script( \'myhandle\', \'mydata\', array( \'ajaxurl\' => admin_url(\'admin-ajax.php\') ) );
}
}
add_action( \'wp_enqueue_scripts\', \'mytheme_custom_scripts\' );
function get_choices() {
$main_category = $_POST[\'main_category\'];
$choices = []; // todo: get choices here
wp_send_json_success($choices); // send data with success code and die
}
add_action(\'wp_ajax_get_choices\', \'get_choices\'); //back-end
add_action(\'wp_ajax_nopriv_get_choices\', \'get_choices\'); //front-end
然后在脚本中进行AJAX调用以获得选择
jQuery(function ($) {
$(document).ready(function() {
$("#main-category").on(\'change\', function() {
$.post(mydata.ajaxurl, { // mydata has been created by the wp_localize_script function
action: \'get_choices\',
main_category: $("#main-category").val()
}, function (response) {
console.log(response)
// todo: display new choices
})
});
});
});