2014-03-25 29 views
0

這裏是我想要做的事:通行證變量的WordPress

在WordPress,我通過wp_localize_script傳遞變量從PHP到外部JavaScript文件(),這部分似乎是工作,但我想使用變量值作爲jQuery函數中的鍵控參數。

使用警報,正確的值似乎被傳遞給JavaScript,但我不知道如何在函數的參數內引用它們。 (具體WideSliderParams.slideshow和速度)

這裏的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. 

在如此重要的情況下,這裏是從的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'); 
+0

實際發生了什麼?你是否說'WideSliderParams.slideshow'不再在傳遞給'flexslider'的選項映射中定義? –

+0

它沒有在選項圖中取值false和5000。它仍然在網站上行事,就好像設置是真實的一樣。 – tangobango

+0

哪個Flexslider是你正在使用的? – brasofilo

回答

0

OK,經過一番詳細測試,我認爲這是一個類型問題。

我傳給我的html/php格式的js的值是一個字符串「true」或「false」,並且都被讀爲布爾值true

現在,我將它們轉換爲布爾值,該函數似乎工作。謝謝大家。