简体   繁体   English

在功能参数值Wordpress中传递变量

[英]Pass variable in function parameter value Wordpress

Here is what I am trying to do: 这是我正在尝试做的事情:

In Wordpress, I am passing a variable from php to an external javascript file via wp_localize_script() and this part seems to be working, but I want to use the variables values as keyed parameters in a jquery function. 在Wordpress中,我通过wp_localize_script()将变量从php传递到外部javascript文件,这部分似乎正常工作,但是我想将变量值用作jquery函数中的键参数。

Using alert, the correct values seem to be passed to the javascript, but I don't know how to reference them inside the function's parameters. 使用alert,正确的值似乎传递给了javascript,但是我不知道如何在函数的参数内引用它们。 (Specifically WideSliderParams.slideshow and speed.) (特别是WideSliderParams.slideshow和速度。)

Here's the JavaScript: 这是JavaScript:

alert (WideSliderParams.slideshow);      // alerts false
alert (WideSliderParams.animationSpeed); // alerts 5

var speed = 1000 * WideSliderParams.animationSpeed;

alert( speed); //alerts 5000

jQuery('#featured-wide').flexslider({
    animation:'slide',
    controlNav:false,
    animationLoop:true,
    slideshow:WideSliderParams.slideshow,
    useCSS:false,
    smoothHeight:true,
    animationSpeed:speed,
    sync: "#featured-wide-thumbnav",
    manualControls: '.wide-custom-controls li a',
    controlsContainer: '.wide-slider',

    before: function(slider) { 
   Etc.

In case it matters, here is the piece from functions.php: 万一重要,这是来自functions.php的部分:

function wpbpp_wide_slider() {
  wp_enqueue_script( 'flex-script-main', get_stylesheet_directory_uri().'/js/flex-script-main.js', array( 'jquery' ), '1.0' );

  $wpbpp_options = get_option ('wpbpp_settings') ;

  $ss = $wpbpp_options['slideshow'];
  $as = $wpbpp_options['animationSpeed'];

  $wpbpp_slider_options = array (
                            'slideshow' => $ss,
                            'animationSpeed' => $as
                            );

  wp_localize_script('flex-script-main', 'WideSliderParams', $wpbpp_slider_options );

}

add_action('wp_enqueue_scripts', 'wpbpp_wide_slider');

OK, after some more testing, I do think it was a type problem. 好的,经过更多测试之后,我认为这是一个类型问题。

The value I was passing to the js coming out of my html/php form was a string "true" or "false", and both were being read as boolean true . 我从html / php表单传递给js的值是字符串“ true”或“ false”,并且都被读取为boolean true

Now that I am casting them as boolean, the function seems to work. 现在,我将它们转换为布尔值,该函数似乎可以正常工作。 Thanks all. 谢谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM