Short version: 我能够使用wp\\u localize\\u脚本将一个PHP变量放入JS文件中。在JS文件中,我可以使用控制台回显变量。日志,但我无法将其回显到jQuery插件的选项中。
Long version:
<插件短代码。
public function slider_shortcode($atts) {
$data = shortcode_atts (
array(
\'slideshow\' => \'\',
\'responsive\' => \'\',
),
$atts
);
$slideshow_att = $data[\'slideshow\'];
// DEBUG -> IT WORKS. I can see $responsive value on the page.
print_r($data[\'responsive\']);
ob_start();
$my_slider( $slideshow=$slideshow_att );
$my_slider_content = ob_get_clean();
wp_enqueue_script( $this->plugin_slug . \'-public-script\', plugins_url( \'assets/js/public.js\', __FILE__ ), array( \'jquery\' ), $this->version, TRUE);
wp_localize_script(
$this->plugin_slug . \'-public-script\',
\'myScriptObject\',
$data
);
return $my_slider_content;
}
输出JS文件。
(function ($) {
"use strict";
$(window).load(function () {
var $resp = myScriptObject.responsive;
// DEBUG -> IT WORKS. I CAN SEE THE SHORTCODE VALUE.
console.log($resp);
$(\'.my-slider\').SuperSlider({
\'fullWidth\': true,
\'responsive\': $resp, // <<-- HERE IS PROBLEM. IT NEVER CHANGES.
\'dimensions\': \'900,300\',
});
});
}(jQuery));
短码
[myslider responsive=true]
值真使用print_r($data[\'responsive\'])
和console.log($resp)
. 但出于某种原因responsive: $resp
do not change!我在这里错过了什么?