2013-04-10 31 views
0

如果這是錯誤的地方,我很抱歉。其實這已經被另一個用戶問過(雖然它不是同一個問題),並且據說這是一個PHP問題。不幸的是我沒有足夠的PHP來實現答案。Functions.php中的全局變量然後在Javascript中使用(WordPress)

這裏是[前一個問題]:https://wordpress.stackexchange.com/questions/82503/cant-get-options-with-datavariable

我面臨同樣的問題。在我的選項中,我有一個滑塊的選項(動畫 - 淡入淡出或幻燈片),然後我想使用存儲在選項中的值,並將其傳遞到我的function.php中的Javascript中。

在選項文件中我有這些代碼:

// Slider animation options 
     $of_options_slider_animation = array(
      "fade" => __("Fade", "themename"), 
      "slide" => __("Slide", "themename") 
     ); 

$of_options[] = array( "name" => __("Slider Animation", "themename"), 
         "desc" => __("Select the type of animation for the slider.", "themename"), 
         "id" => "slider_animation", 
         "std" => "fade", 
         "type" => "radio", 
         "options" => $of_options_slider_animation 

後來我會通過選項進入一個代碼塊的JS中的functions.php像這樣:

jQuery('.flexslider').flexslider({ 
        controlNav: true, 
        directionNav: true, 
        prevText: 'Previous',  
        nextText: 'Next', 
        **animation: "<?php echo $smof_data['slider_animation']; ?>",** 
        animationLoop: false 
        // animation: "slide" 
        }); 

(請注意粗體部分)

但是,正如您可能預測的那樣,它不起作用。

我試着定義上一個問題中的$ smof_data(參見上面的鏈接),但仍然沒有運氣。這是我做的:

// Fetch options data 
global $smof_data; 
$smof_data = of_get_options("slider_animation"); 

請幫幫忙,在此先感謝:)

編輯:

SMOF鏈接:https://github.com/sy4mil/Options-Framework

我使用的是空白/啓動主題用下劃線。我

+0

但是,這個Javascript如何被嵌入?它是外部的'.js'還是由PHP內聯打印? – brasofilo 2013-04-10 23:22:45

+0

嗯..說實話我不是專家,所以我不知道正確的答案..但它是FlexSlider腳本(WooTheme的),如你所知,我們需要把「jQuery(document).ready(function($ )「,因此我將.ready部分掛在了functions.php的腳註中,其餘的腳本/插件都在其他文件中 – Mario88 2013-04-10 23:29:23

回答

1

解決了它:DI直接在Javascript範圍內使用變量。這裏是代碼(以防萬一)

function mytheme_flexslider() { 

    if (!is_admin()) { 

     // Enqueue FlexSlider JavaScript 
     wp_register_script('jquery_flexslider', get_template_directory_uri(). '/js/jquery.flexslider-min.js', array('jquery')); 
     wp_enqueue_script('jquery_flexslider'); 

     // Enqueue FlexSlider Stylesheet   
     wp_register_style('flexslider-style', get_template_directory_uri() . '/inc/flexslider/flexslider.css', 'all'); 
     wp_enqueue_style('flexslider-style'); 

     // FlexSlider custom settings  
     add_action('wp_footer', 'mytheme_flexslider_settings'); 

     function mytheme_flexslider_settings() { 
     // Fetch options data 
     **global $smof_data;?>** 

      <script> 

       // Can also be used with $(document).ready() 
       // flexslider have a fixed height 
       jQuery(window).load(function() { 
        // jQuery('.subhead_shadow_bottom').hide(); 
        jQuery('.flexslider').flexslider({ 
        controlNav: true, 
        directionNav: true, 
        prevText: 'Previous',  
        nextText: 'Next', 
        animation: "<?php echo $smof_data['slider_animation']; ?>", 
        animationLoop: false 
        // animation: "slide" 
        }); 
       }); 


       jQuery(document).ready(function() { 
        fixFlexsliderHeight(); 
       }); 

       jQuery(window).load(function() { 
        fixFlexsliderHeight(); 
       }); // BUG: this ends up computing the slide height to the image height, not to the resized height, on page reload 

       jQuery(window).resize(function() { 
        fixFlexsliderHeight(); 
       }); 


       function fixFlexsliderHeight() { 
        // Set fixed height based on the tallest slide 
        jQuery('.flexslider').each(function(){ 
         var sliderHeight = 0; 
         jQuery(this).find('.slides > li').each(function(){ 
          slideHeight = jQuery(this).height(); 
          if (sliderHeight < slideHeight) { 
           sliderHeight = slideHeight; 
          } 
         }); 
         // jQuery(this).find('ul.slides').css({'height' : sliderHeight}); 
         // console.log("Fixing slider height to " + sliderHeight); 
        }); 
       } 

       // jQuery(document).ready(function($){ 

       // $('.flexslider').flexslider(); 
       // }); 
      </script> 
     <?php 
     **// return $smof_data;** 
     } 

    } 
} 

add_action('init', 'mytheme_flexslider'); 

所有正在工作。也許有一個小問題:我是否需要返回$ smof_data(第二個粗體部分)?它適用於兩種方式..我需要了解更多關於變量範圍的信息。

+0

哦,還有,js應該讓滑塊的高度爲但是在我們刷新頁面後它不工作,想知道是否有另一個工作解決方案。 – Mario88 2013-04-10 23:37:27