简体   繁体   中英

WordPress Customizer Hide Background Image Checkbox

I'm trying to add a checkbox to the Customizer to "Display Background Image". When unchecked, it should set background-image: none to that element. I can't see where I'm going wrong.

This is what I have in my customizer.php file

// Background Image         
$wp_customize->add_setting( 'background_image', array(
    'default'           => get_template_directory_uri() . '/images/default.jpg',
    'transport'         => 'postMessage'
) );    
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'background_image', array(
    'label'             => __( 'Background Image', 'minitheme' ),
    'settings'          => 'background_image'
) ) );

// Display Background Image 
$wp_customize->add_setting( 'display_background_image', array(
    'default'           => true,
    'transport'         => 'postMessage'
) );
$wp_customize->add_control( 'display_background_image', array(
    'label'             => __( 'Display Background Image', 'minitheme' ),
    'type'              => 'checkbox'
) );

...

public static function header_output() {
    ?>
    <!--Customizer CSS--> 
    <style type="text/css">
        <?php self::generate_css( '#header', 'background-image', 'slide_1_background_image' ); ?>
        #slide-1 {display: <?php 
            $display = get_theme_mod('display_slide_1_background_image', '1');
            if ('false') {echo 'none';}
        ?>; }
    </style> 
    <!--/Customizer CSS-->
    <?php
}

THis is the code I have in my corresponding customizer.js file:

// Background Image
wp.customize( 'background_image', function( value ) {
    value.bind( function( newval ) {
        $('#slide-1').css('background-image', 'url("' + newval + '")' );
    } );
} );

// Display Background Image
wp.customize( 'display_background_image', function( value ) {
    value.bind( function( to ) {
        if ( false === to ) {
            $( '#header' ).css('background-image', 'none' );
        }
    });
});

尝试从“ background_image”设置中删除默认值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM