如果布尔值为true 然后它会一直回来1 或者如果布尔值为false 然后它就会回来empty 这就是为什么你1 如果true.
下面的代码在我的本地项目上进行了测试。复制并粘贴它
//Add it in **functions.php**
function load_localize_scripts() {
wp_enqueue_script(\'localize_script\', get_template_directory_uri() . \'/js/localize_script.js\', array(), \'1.0.0\', true );
wp_localize_script(\'localize_script\', \'localize_scripts_vars\', array(
\'boolean\' => true, // it will return 1
\'integer\' => 10 // it will always return integre
)
);
}
add_action(\'wp_enqueue_scripts\', \'load_localize_scripts\');
//Add this in **localize_script.js**
jQuery(document).ready(function() {
var site_config;
if (localize_scripts_vars.boolean == \'1\') {
site_config = {"boolean":\'true\',"number":localize_scripts_vars.integer};
console.log(site_config);
} else if (localize_scripts_vars.boolean == \'\' || localize_scripts_vars.boolean == NULL) {
site_config = {"boolean":\'false\',"number":localize_scripts_vars.integer};
console.log(site_config);
};
});
您也可以看到控制台结果