使用复选框在定制器中创建从属文本字段

时间:2016-06-03 作者:Schalk Joubert

仅当选中复选框时,我才尝试在自定义程序中显示文本区域。我遵循了这篇帖子的指示,但我的文本区域一直隐藏着。任何帮助都将不胜感激!非常感谢。

function map_enabled(){
$tsum_options = get_theme_mod( \'tsum_options\');
if( empty( $tsum_options[\'map_code\'] ) ) {
    return false;
}
return true;
}


function tsum_customize_register( $wp_customize ) {

$wp_customize->add_setting( \'tsum_options[show_map]\', array(
    \'default\'    => false,
    \'capability\' => \'edit_theme_options\',
    ));

    // FRONT BOX CHECK - CONTROL
    $wp_customize->add_control( \'show_map\', array(
    \'label\'      => \'Show Goole Map?\',
    \'section\'    => \'tsum_contact_section\',
    \'settings\'   => \'tsum_options[show_map]\',
    \'type\'       => \'checkbox\',
    //\'priority\'   => 20,
    ));  

    // FRONT BOX TEXT - SETTING
    $wp_customize->add_setting(\'tsum_options[map_code]\', array(
    \'default\'        => \'<iframe src=... \',
    \'capability\'           => \'edit_theme_options\',
    ));

    // FRONT BOX TEXT - CONTROL
    $wp_customize->add_control(\'map_code\', array(
    \'label\'   => \'lll\',
    \'description\' => \'To embed a map, follow the steps below.<br>
                        1.) Open Google Maps. <a href="https://www.google.com/maps" target="_blank">Click here.</a><br>
                        2.) Search for your Location.<br>
                        3.) Zoom in - optional.<br>
                        4.) Change to Earth View - optional.<br>
                        5.) Click on Share<br>
                        6.) Click on Embed<br>
                        7.) Select Size => Custom Size.<br>
                        8.) Type in: 980  x  300<br>
                        9.) Copy the iframe code<br>
                        10.) Paste it below<br>
                        11.) Click on Save and Publish\',  
    \'section\' => \'tsum_contact_section\',
    \'type\'    => \'textarea\',
    \'setting\' => \'tsum_options[map_code]\',
    \'active_callback\' => \'map_enabled\',
    )); 
}
add_action( \'customize_register\', \'tsum_customize_register\' );

2 个回复
SO网友:cjbj

在第二行中,您尝试使用以下工具获取所有mod的数组:

$tsum_options = get_theme_mod( \'tsum_options\');

这将始终返回unset,因为如果您想要所需的所有mod(请注意复数):

$tsum_options = get_theme_mods( \'tsum_options\');

或者,您可以立即使用

$tsum_option = get_theme_mod( \'tsum_options[show_map]\');

SO网友:majick

也许你可以试试这样的方法,即使用$control 对象以获取活动值而不是存储的值:

function map_enabled($control) {
    $option = $control->manager->get_setting(\'tsum_options[show_map]\');
    if (empty($option->value()) {return false;}
    return true;
}

相关推荐