如何在定制器中创建依赖字段?

时间:2015-05-15 作者:Nilambar Sharma

我正在尝试在Customizer中具有依赖字段。一个字段是复选框Enable Custom Excerpt Length. 另一个是文本字段Custom Excerpt Length. 我想使用实现上下文字段active_callback. 我正在关注这篇文章。https://make.wordpress.org/core/2014/07/08/customizer-improvements-in-4-0/

我在检查回调函数中的控制值时遇到问题。

  $wp_customize->add_setting( \'blueplanet_options[theme_enable_custom_excerpt]\',
     array(
        \'default\'    => false,
        \'capability\' => \'edit_theme_options\',
     )
  );
  $wp_customize->add_control(
      \'theme_enable_custom_excerpt\',
      array(
        \'label\'       => \'Enable Custom Excerpt Length\',
        \'section\'     => \'admin_section\',
        \'settings\'    => \'blueplanet_options[theme_enable_custom_excerpt]\',
        \'type\'        => \'checkbox\',
      )
    );
  $wp_customize->add_setting( \'blueplanet_options[theme_custom_excerpt_length]\',
     array(
        \'default\'              => 20,
        \'capability\'           => \'edit_theme_options\',
     )
  );
  $wp_customize->add_control(
      \'theme_custom_excerpt_length\',
      array(
        \'label\'           => \'Custom Excerpt Length\',
        \'section\'         => \'admin_section\',
        \'settings\'        => \'blueplanet_options[theme_custom_excerpt_length]\',
        \'type\'            => \'text\',
        \'active_callback\' => \'flag_is_custom_excerpt_enabled\',
      )
    );

  // Callback function
  function flag_is_custom_excerpt_enabled(){
    // how to check if `theme_enable_custom_excerpt` is enabled or disabled
  }

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

你可以像往常一样在任何地方获取和检查你的主题模型的值。

此代码经过测试并正常工作(内部代码cyb_customizer() 正是您在问题中发布的代码,只添加了add_section 零件号):

function flag_is_custom_excerpt_enabled(){
    $blueplanet_options = get_theme_mod( \'blueplanet_options\');
    if( empty( $blueplanet_options[\'theme_enable_custom_excerpt\'] ) ) {
        return false;
    }
    return true;
}

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


    $wp_customize->add_section(
        \'admin_section\',
        array(
            \'title\' => \'Admin section\',
            \'description\' => \'Admin section\',
            \'priority\' => 0,            
        )
    );

    $wp_customize->add_setting( \'blueplanet_options[theme_enable_custom_excerpt]\',
        array(
            \'default\'    => false,
            \'capability\' => \'edit_theme_options\',
         )
      );
      $wp_customize->add_control(
          \'theme_enable_custom_excerpt\',
          array(
            \'label\'       => \'Enable Custom Excerpt Length\',
            \'section\'     => \'admin_section\',
            \'settings\'    => \'blueplanet_options[theme_enable_custom_excerpt]\',
            \'type\'        => \'checkbox\',
          )
       );
      $wp_customize->add_setting( \'blueplanet_options[theme_custom_excerpt_length]\',
         array(
            \'default\'              => 20,
            \'capability\'           => \'edit_theme_options\',
         )
      );
      $wp_customize->add_control(
          \'theme_custom_excerpt_length\',
          array(
            \'label\'           => \'Custom Excerpt Length\',
            \'section\'         => \'admin_section\',
            \'settings\'        => \'blueplanet_options[theme_custom_excerpt_length]\',
            \'type\'            => \'text\',
            \'active_callback\' => \'flag_is_custom_excerpt_enabled\',
          )
        );

    }

结束

相关推荐