4

我有一个函数可以在我的 WordPress 主题中初始化一个图像滑块,但是我无法将 PHP 变量传递给它。这是代码:

function slideshowSettings ($pause_time) {
$code = "<script>
jQuery(function(){
    jQuery('#camera_wrap_3').camera({
        height: '40%',
        thumbnails: true,
        time: ".$pause_time.",
        fx: '".$transition_effect."',
        transPeriod: ".$transition_speed.",
        autoAdvance: ".$auto_advance.",
        minHeight: '50px',
        mobileNavHover: false,
        imagePath: '".get_template_directory_uri()."/images/'
    });
});
</script>";

echo $code;
}
add_action('wp_head', 'slideshowSettings');

变量分配在函数上方,但我从函数获得的输出如下所示:

<script>
jQuery(function(){

    jQuery('#camera_wrap_3').camera({
        height: '40%',
        thumbnails: true,
        time: ,
        fx: '',
        transPeriod: ,
        autoAdvance: ,
        minHeight: '50px',
        mobileNavHover: false,
        imagePath: 'http://www.brainbuzzmedia.com/themes/simplybusiness/wp-content/themes/simplybusiness/images/'
    });
});
</script>

我怎样才能传递这些变量?

4

3 回答 3

3

您不能向函数添加参数,因为当函数调用wp_head时,没有任何参数传递给您的钩子函数。的论据是do_action('wp_head');wp_head()add_action()

  1. 挂钩的动作,在你的情况下是“wp_head”
  2. 您要执行的功能,在您的情况下为“slideshowSettings”
  3. 执行的优先级,默认为10
  4. 您的函数接受的参数数量(但是必须通过这些参数do_action

如果您需要能够在挂钩函数之外将这些值传递给wp_head,我将使用它apply_filters来修改一个值:

function slideshowSettings(){
    // set up defaults
    $settings = array('pause_time'=>10, 'other'=>999);
    $random_text = "foo";

    // apply filters
    $settings = apply_filters('slideshow_settings', $settings, $random_text);

    // set array key/values to variables
    extract( $settings );

    // will echo 1000 because value was updated by filter
    echo $pause_time;

    // will echo "foobar" because key was added/updated by filter
    echo $random_text; 

    // ... more code
}
add_action( 'wp_head', 'slideshowSettings' );

function customSettings($settings, $random_text){
    // get your custom settings and update array
    $settings['pause_time'] = 1000;
    $settings['random_text'] = $random_text . "bar";
    return $settings;
}
// add function to filter, priority 10, 2 arguments ($settings array, $random_text string)
add_filter( 'slideshow_settings', 'customSettings', 10, 2 );
于 2012-11-08T04:56:09.310 回答
3

将 PHP 变量传递给 Javascript 的正确方法是使用wp_localize_script.

查看Otto 的这篇文章。还有这个WordPress StackExhange 的问答


在您的问题中,您没有显示这些变量的来源,因此它们正在打印......什么也没有。

我刚做了一个测试。这将在数​​据库中创建选项值。

add_action( 'init', 'so_13282503_init' );

function so_13282503_init() 
{
    if( !get_option('pause_time') )
    {
        update_option('pause_time', '50');
        update_option('transition_effect', 'elastic.easeIn');
        update_option('transition_speed', '400');
        update_option('auto_advance', '4');
    }
}

这是您的代码的改编版本:

add_action( 'wp_head', 'slideshowSettings' );

function slideshowSettings () 
{
    $pause_time = get_option('pause_time');
    $transition_effect = get_option('transition_effect');
    $transition_speed = get_option('transition_speed');
    $auto_advance = get_option('auto_advance');
    $code = "<script>
    jQuery(function(){
        jQuery('#camera_wrap_3').camera({
           height: '40%',
            thumbnails: true,
            time: ".$pause_time.",
            fx: '".$transition_effect."',
            transPeriod: ".$transition_speed.",
            autoAdvance: ".$auto_advance.",
            minHeight: '50px',
            mobileNavHover: false
        });
    });
</script>
";

    echo $code;
}

这导致在<head>站点中打印此内容:

呈现的 html 中的代码结果

于 2012-12-03T04:13:49.883 回答
1

您可以在add_action()中的优先级参数之后传递参数的数量

 <?php add_action( $tag, $function_to_add, $priority, $accepted_args ); ?> 

wordpress codex 有一些关于它的例子。

您应该注意,您的函数实际上只接受参数,并且您正在使用未定义的变量,例如 $transition_effect,$transition_speed$auto_advance在该函数中。

于 2012-11-08T04:44:36.543 回答