如果你来错地方了,我很抱歉。实际上,之前已经有另一个用户问过这个问题(尽管这不是同一个问题),据说这是一个PHP问题。不幸的是,我在PHP中做得不够好,无法实现答案。
下面是前一个问题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稍后,我会将选项传递到functions.php中的js代码块中,如下所示:
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
我使用的是underscores.me的空白/起始主题
发布于 2013-04-11 07:34:01
解决了这个问题:D我直接在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 (第二个粗体部分)吗?它是双向工作的..我需要了解更多关于可变作用域的知识..
https://stackoverflow.com/questions/15938017
复制相似问题