我的主题中有一个自定义选项,它有两个部分:下拉菜单和文本区域。下拉菜单有三个选项:活动、暂停和取消。我试图找到一种方法,只有在选择了“Suspended”时才隐藏文本区域。
我在网上找了一些active_callback
但我不熟悉这种回调。
迄今为止的代码:
function twsa_customizer( $wp_customize ) {
// Show schedule
$wp_customize->add_setting( \'twsa_show_schedule\' );
$wp_customize->add_control( \'twsa_show_schedule\', array(
\'type\' => \'select\',
\'label\' => \'Show status\',
\'section\' => \'twsa_show\',
\'choices\' => array(
\'active\' => \'Active\',
\'suspended\' => \'Suspended\',
\'cancelled\' => \'Cancelled\',
),) );
$wp_customize->add_setting(\'twsa_show_suspend\');
$wp_customize->add_control(\'twsa_show_suspend\', array(
\'description\' => \'Reason for suspension\',
\'section\' => \'twsa_show\',
\'type\' => \'textarea\',
\'active_callback\' => \'estore_is_cart\',
));
}
add_action( \'customize_register\', \'twsa_customizer\', 20 );
最合适的回答,由SO网友:Weston Ruter 整理而成
在本例中,estore_is_cart
应返回true
只有\'suspended\' === $wp_customize->get_setting( \'twsa_show_schedule\' )->value()
. 或者换句话说:
$wp_customize->add_control( \'twsa_show_suspend\', array(
\'description\' => \'Reason for suspension\',
\'section\' => \'twsa_show\',
\'type\' => \'textarea\',
\'active_callback\' => function() use ( $wp_customize ) {
return \'suspended\' === $wp_customize->get_setting( \'twsa_show_schedule\' )->value();
},
));