定制器问题:仅在部分中显示最后一个字段

时间:2018-03-22 作者:Salvio

我正在使用定制器创建两个颜色控件。

function main_salvio_customizer( $wp_customize ) {

    $wp_customize->add_setting( \'page-title-section\' , array(
        \'default\'   => \'#3c93bd\',
        \'transport\' => \'postMessage\',
    ) );

    $wp_customize->add_setting( \'page-title-section-text-color\' , array(
        \'default\'   => \'#000000\',
        \'transport\' => \'postMessage\',
    ) );

    $wp_customize->add_section( \'personalize-color-page\' , array(
    \'title\'      => __( \'Visible Section Name\' ),
    \'priority\'   => 30,
    ) );

     $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'link_color\', array(
    \'label\'      => __( \'Page Text Color\' ),
    \'settings\'   => \'page-title-section-text-color\',
        \'section\'    => \'personalize-color-page\',
    ) ) ); 

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'link_color\', array(
    \'label\'      => __( \'Page Title Control\' ),
    \'settings\'   => \'page-title-section\',
        \'section\'    => \'personalize-color-page\',
    ) ) );  
}
add_action( \'customize_register\', \'main_salvio_customizer\' );
问题是,如果出现“更改订单页文本颜色”而另一个控件消失,我只能看到最后添加的控件。

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

WP_Customize_Color_Control 采用3个参数:

/**
 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
 * @param string $id Control ID.
 * @param array $args Optional. Arguments to override class property defaults.
 */
原始代码的问题是您传递了相同的控件ID(link_color) 到两个单独的实例。因此,最近添加的控件将使用相同的控件ID踩在前一个控件上。请确保为控件提供唯一的ID。

下面是修改后的代码,每个颜色控件都有唯一的控件ID:

add_action( \'customize_register\', \'main_salvio_customizer\' );
function main_salvio_customizer( $wp_customize ) {

    $wp_customize->add_section( \'personalize-color-page\', array(
        \'title\'         => __( \'Visible Section Name\', \'text-domain\' ),
        \'priority\'  => 30,
    ) );

    $wp_customize->add_setting( \'page-title-section\', array(
            \'default\'       => \'#3c93bd\',
            \'transport\' => \'postMessage\',
    ) );

    $wp_customize->add_setting( \'page-title-section-text-color\' , array(
            \'default\'   => \'#000000\',
            \'transport\' => \'postMessage\',
    ) );

    // Control ID page_text_color
    $wp_customize->add_control(
        new WP_Customize_Color_Control( $wp_customize, \'page_text_color\', array(
            \'label\'    => __( \'Page Text Color\', \'text-domain\' ),
            \'settings\' => \'page-title-section-text-color\',
            \'section\'  => \'personalize-color-page\',
        ) )
    ); 

    // Control ID page_title_color
    $wp_customize->add_control(
        new WP_Customize_Color_Control( $wp_customize, \'page_title_color\', array(
            \'label\'    => __( \'Page Title Control\', \'text-domain\' ),
            \'settings\' => \'page-title-section\',
            \'section\'    => \'personalize-color-page\',
        ) )
    );  
}

结束

相关推荐