我也遇到了同样的问题,通过谷歌搜索,我找到了以下解决方案:
// Add the selective part
$wp_customize->selective_refresh->add_partial( \'your_theme_second_logo\', array(
\'selector\' => \'#yourID\', // You can also select a css class
) );
添加自定义设置后,需要告诉WordPress要通过选择性刷新管理哪些设置。
例如,我想在主徽标旁边添加第二个徽标,因此我添加了以下代码:
/* Customizer fields */
function your_theme_customizer_settings($wp_customize) {
// Add a setting to upload partners logo, right next to our logo
$wp_customize->add_setting(\'your_theme_second_logo\');
// Add the field informations
$wp_customize->add_control(
new WP_Customize_Image_Control(
$wp_customize,
\'your_theme_second_logo\',
array(
\'label\' => __(\'Upload second \', \'textdomain\'),
\'description\' => __( \'Some description\', \'textdomain\'),
\'section\' => \'title_tagline\',
\'settings\' => \'your_theme_second_logo\',
)
)
);
// Add the selective part
$wp_customize->selective_refresh->add_partial( \'your_theme_second_logo\', array(
\'selector\' => \'#yourID\', // You can also select a css class
) );
}
// Customizer action
add_action(\'customize_register\', \'your_theme_customizer_settings\');
完成所有后端部分后,需要移动到前端。为了在字段为空的情况下有一个可见的编辑按钮(在这种情况下,没有选择图片),您需要将字段包装到一些包装器中。我使用了一个span包装器
yourID 身份证件
<span id="yourID">
<?php /* Partner logo */
$headPartner = get_theme_mod( \'your_theme_second_logo\' );
if ( !empty($headPartner) ) {
echo \'<img src="\' . $headPartner . \'" alt="Your Partner">\';
}
?>
</span>