WP_Customize_Background_Image_Control应该如何工作?

时间:2013-01-18 作者:Daniel

我正试图用这段代码向我的主题定制器添加第二个背景图像函数。

    // HTML Background Image
$wp_customize->add_setting(
    \'html_background_image\', 
    array(
        \'capability\'     => \'edit_theme_options\',
));

$wp_customize->add_control( 
    new WP_Customize_Background_Image_Control(
        $wp_customize,
        \'background_image\', 
        array(
            \'label\'    => __(\'HTML Background Image\', \'TEXTDOMAIN\'),
            \'section\'  => \'background_image\',
            \'settings\' => \'html_background_image\',
)));
但它不起作用。我错过什么了吗?

1 个回复
SO网友:editor

关于这个类的文档很少,但下面是WordPress的完整示例core (这是github). 注意,您将使用$wp_customize 对象作为custom_register 回调和非回调$this 核心类也是如此。我在下面做了一个搜索和替换,结果很好。

        /* CORE COPY PASTE */
        //https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-customize-manager.php#L808

        /* Custom Background */

        $wp_customize->add_section( \'background_image\', array(
            \'title\'          => __( \'Background Image\' ),
            \'theme_supports\' => \'custom-background\',
            \'priority\'       => 80,
        ) );

        $wp_customize->add_setting( \'background_image\', array(
            \'default\'        => get_theme_support( \'custom-background\', \'default-image\' ),
            \'theme_supports\' => \'custom-background\',
        ) );

        $wp_customize->add_setting( new WP_Customize_Background_Image_Setting( $wp_customize, \'background_image_thumb\', array(
            \'theme_supports\' => \'custom-background\',
        ) ) );

        $wp_customize->add_control( new WP_Customize_Background_Image_Control( $wp_customize ) );

        $wp_customize->add_setting( \'background_repeat\', array(
            \'default\'        => \'repeat\',
            \'theme_supports\' => \'custom-background\',
        ) );

        $wp_customize->add_control( \'background_repeat\', array(
            \'label\'      => __( \'Background Repeat\' ),
            \'section\'    => \'background_image\',
            \'type\'       => \'radio\',
            \'choices\'    => array(
                \'no-repeat\'  => __(\'No Repeat\'),
                \'repeat\'     => __(\'Tile\'),
                \'repeat-x\'   => __(\'Tile Horizontally\'),
                \'repeat-y\'   => __(\'Tile Vertically\'),
            ),
        ) );

        $wp_customize->add_setting( \'background_position_x\', array(
            \'default\'        => \'left\',
            \'theme_supports\' => \'custom-background\',
        ) );

        $wp_customize->add_control( \'background_position_x\', array(
            \'label\'      => __( \'Background Position\' ),
            \'section\'    => \'background_image\',
            \'type\'       => \'radio\',
            \'choices\'    => array(
                \'left\'       => __(\'Left\'),
                \'center\'     => __(\'Center\'),
                \'right\'      => __(\'Right\'),
            ),
        ) );

        $wp_customize->add_setting( \'background_attachment\', array(
            \'default\'        => \'fixed\',
            \'theme_supports\' => \'custom-background\',
        ) );

        $wp_customize->add_control( \'background_attachment\', array(
            \'label\'      => __( \'Background Attachment\' ),
            \'section\'    => \'background_image\',
            \'type\'       => \'radio\',
            \'choices\'    => array(
                \'fixed\'      => __(\'Fixed\'),
                \'scroll\'     => __(\'Scroll\'),
            ),
        ) );
您似乎无法自定义例如部分,有关证据,请参阅此代码中的第三个参数WP_Customize_Image_Control

结束

相关推荐