我有以下两个自定义控件,它们显示用于显示/隐藏内容的复选框。每个复选框后面都有一个active_callback
选中/取消选中复选框时显示/隐藏相应控件的。
// Display About Block Button
$wp_customize->add_setting( \'display_about_block_button\', array(
\'default\' => true
) );
$wp_customize->add_control( \'display_about_block_button\', array(
\'label\' => __( \'Display About Button\', \'my_theme_name\' ),
\'type\' => \'checkbox\',
\'section\' => \'about\',
\'active_callback\' => \'display_about_block_callback\'
) );
function display_about_block_button_callback( $control ) {
if ( $control->manager->get_setting( \'display_about_block_button\' )->value() == true ) {
return true;
} else {
return false;
}
}
// Display About Block
$wp_customize->add_setting( \'display_about_block\', array(
\'default\' => true
) );
$wp_customize->add_control( \'display_about_block\', array(
\'label\' => __( \'Display About Block\', \'my_theme_name\' ),
\'type\' => \'checkbox\',
\'section\' => \'about\'
) );
function display_about_block_callback( $control ) {
if ( $control->manager->get_setting( \'display_about_block\' )->value() == true ) {
return true;
} else {
return false;
}
}
除了问题是
About Block Button
下面是
About Block
. 我希望能够隐藏
About Block Button
如果
About Block
显示,但也可以隐藏
About Block Button
如果
About Block
完全隐藏(无需选中两个框)。因此,它需要两个
active_callback
\'是这样的:
// About Block Button Text
$wp_customize->add_setting( \'about_block_button_text\', array(
\'default\' => __( \'Read More\', \'my_theme_name\' )
) );
$wp_customize->add_control( \'about_block_button_text\', array(
\'label\' => __( \'About Button Text\', \'my_theme_name\' ),
\'type\' => \'text\',
\'section\' => \'about\',
\'active_callback\' => \'display_about_block_button_callback\',
\'active_callback\' => \'display_about_block_callback\'
) );
不幸的是,这两者都不能接受
active_callback
\'s、 如何在一个控件上有两个(或可能更多)活动回调?
最合适的回答,由SO网友:Weston Ruter 整理而成
您可以创建第三个只引用两个现有回调函数的活动回调函数,例如通过匿名函数(PHP≥5.3):
// About Block Button Text
$wp_customize->add_setting( \'about_block_button_text\', array(
\'default\' => __( \'Read More\', \'my_theme_name\' )
) );
$wp_customize->add_control( \'about_block_button_text\', array(
\'label\' => __( \'About Button Text\', \'my_theme_name\' ),
\'type\' => \'text\',
\'section\' => \'about\',
\'active_callback\' => function( $control ) {
return (
display_about_block_button_callback( $control )
&&
display_about_block_callback( $control )
);
},
) );