Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

javascript - Background image not saving in WordPress Customizer

I'm trying to set a background image in the WordPress Customizer. I can upload the image and see it preview in Customizer but after I save it, its not appearing on the live site.

I have the following code in my customizer.php file:

$wp_customize->add_setting( 'section_1_background_image', array(
    'default'           => get_template_directory_uri() . '/images/default.jpg',
       'transport'      => 'postMessage',
) );    
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'section_1_background_image_control', array(
    'label'             => __('Background Image','minisite'),
    'settings'          => 'section_1_background_image',
    'section'           => 'section_1',
    'priority' => 10,
) ) );

and the corresponding code in my customizer.js file

wp.customize( 'section_1_background_image', function( value ) {
    value.bind( function( newval ) {
        $('#wrapper-1').css('background-image', newval );
    } );
} );

This same setup works fine for background colors but I believe it has to do with "url" needing to be output in the css in front of the background-image filename, which it isn't doing.

I also tried the following with no success:

wp.customize( 'section_1_background_image', function( value ) {
    value.bind( function( newval ) {
        $('#wrapper-1').css('background-image', 'url("' + newval + '")' );
    } );
} );

Am I missing something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're referring to the option to add background images when you navigate to Appearance -> Customize in WordPress' admin I typically provide the option to customize the background through custom-background in my functions.php:

$custom_background = array(
    'default-color'             => '00ff00',
    'default-image'             => get_template_directory() . '/img/default_background.png',
    'background-repeat'         => 'tile',
    'default-position-x'        => 'center',
    'background-attachment'     => 'fixed',
    'wp-head-callback'          => '_custom_background_cb'
);
add_theme_support( 'custom-background', $custom_background );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...