如果自定义程序已引导(即customize_register
动作已启动),然后您可以查看控件本身(假设您翻转choices
数组,因为我认为您的值应该是键,反之亦然):
$price = $wp_customize->get_setting( \'price1\' )->value();
$control = $wp_customize->get_control( \'price1\' );
$price_text = $control->choices[ $price ];
然而,您可能希望在外部获取所选价格的文本,即使定制程序没有启动。在这种情况下,您应该单独存储choices数组,然后将其作为
choices
但也可以在其他地方重用它。例如:
$wpse_250302_default_price_choice = \'95\';
$wpse_250302_price_choices = array(
\'95\' => \'regular price\',
\'65\' => \'special price\',
\'35\' => \'other price\',
);
add_action( \'customize_register\', function( WP_Customize_Manager $wp_customize ) {
global $wpse_250302_default_price_choice, $wpse_250302_price_choices;
$wp_customize->add_setting( \'price1\', array(
\'default\' => $wpse_250302_default_price_choice,
) );
$wp_customize->add_control( \'price1\', array(
\'label\' => \'price 1\',
\'settings\' => \'price1\',
\'section\' => \'pricessection\',
\'type\' => \'select\',
\'choices\' => $wpse_250302_price_choices,
) );
} );
add_action( \'wpse_250302_some_theme_action\', function() {
global $wpse_250302_default_price_choice, $wpse_250302_price_choices;
$price_value = get_theme_mod( \'price1\', $wpse_250302_default_price_choice );
$price_text = $wpse_250302_price_choices[ $price_value ];
printf( "<h4>%s: %s</h4>", esc_html( $price_text ), esc_html( $price_value ) );
} );