使用customizer,在函数中添加这段代码。php
add_action( \'customize_register\' , \'my_theme_options\' );
function my_theme_options( $wp_customize ) {
$wp_customize->add_section(\'mytheme_section_bg_img\', array(
\'title\' => __( \'Section name\', \'my_theme_slug\' ),
\'priority\' => 100,
\'capability\' => \'edit_theme_options\',
\'description\' => __(\'Select a background image\', \'my_theme_slug\'),
)
);
$wp_customize->add_setting(\'section_bg_img\');
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, \'section_bg_img\', array(
\'label\' => __( \'Background image\', \'my_theme_slug\' ),
\'section\' => \'mytheme_section_bg_img\',
\'settings\' => \'section_bg_img\',
\'priority\' => 10,
)
));
}
您必须通过主题slug更改“my\\u theme\\u slug”。
完成后,必须从customizer中选择一个图像,最后使用以下代码将其打印到任何需要的位置:
<div<?php
if ( $section_bg_img = get_theme_mod( \'section_bg_img\' ) )
echo \' style="background-image: url(\' . $section_bg_img . \');"\';
?>></div>
希望有帮助!