【问题标题】:How to get the values of Wordpress customize checkboxes如何获取 Wordpress 自定义复选框的值
【发布时间】:2014-04-03 13:53:40
【问题描述】:

我不知道如何从 WP 自定义管理器中的复选框中获取值 - 无论它们是否被选中。

这是functions.php中的代码:

$wp_customize->add_setting('social_facebook', array(
    'type'       => 'option',
));

$wp_customize->add_control(
    new WP_Customize_Control(
        $wp_customize,
        'social_facebook',
        array(
            'label'          => __( 'Facebook', 'theme_name' ),
            'section'        => 'social-icons',
            'settings'       => 'social_facebook',
            'type'           => 'checkbox',
        )
    )
);

这就是我尝试获取价值的方式:

<?php
$facebook = get_theme_mod('social_facebook');
if ($facebook != ''){?>
<style>
    .facebook {display:inline!important;}
</style>
<?php }
?>

复选框的值要么是“”(空)要么是“1”,因此系统会记录它们的检查。但是,我不知道如何通过 get_theme_mod 方法获取值。此外,它们没有任何名称值,因此我也无法通过通常的方式获取值。

【问题讨论】:

  • get_theme_mod() 适用于 set_theme_mod() 而不是设置 API
  • 那么,你建议我做什么?
  • get_theme_mod() 确实适用于设置 API,但前提是设置类型为 theme_mod(或未指定)。这里设置类型指定为option,所以需要get_option()来取值。

标签: wordpress checkbox customization


【解决方案1】:
$sticky_mod = get_theme_mod( 'wca_header_section_sticky' ) == '1' ? 'sticky' : '';

这是我的例子 - 如果选中该选项,它将在我的模板中回显一个“粘性”类。

【讨论】:

    【解决方案2】:

    如果设置“my_theme_settings[social_facebook]”复选框未选中:

    <?php if( get_theme_mod( 'my_theme_settings[social_facebook]' ) == '') { ?>
    
     //if setting is unchecked content here will be shown
    
    <?php } // end if ?>
    

    全文见:http://themefoundation.com/wordpress-theme-customizer

    【讨论】:

      【解决方案3】:

      尝试使用并自定义它(在functions.php中测试过:

      function mytheme_customize_register( $wp_customize ){
        $wp_customize->add_section(
        // ID
        'layout_section',
        // Arguments array
        array(
          'title' => __( 'Layout', 'my_theme' ),
          'capability' => 'edit_theme_options',
          'description' => __( 'social needs ;)', 'my_theme' )
        )
       );
      
       $wp_customize->add_setting(
        // ID
        'my_theme_settings[social_facebook]',
        // Arguments array
        array('type' => 'option')
        );
      
       $wp_customize->add_control(
        // ID
        'layout_control',
      array(
        'type' => 'checkbox',
        'label' => __( 'Facebook', 'my_theme' ),
        'section' => 'layout_section',
        // This last one must match setting ID from above
        'settings' => 'my_theme_settings[social_facebook]'
       )
       );
      }
      
      add_action( 'customize_register', 'mytheme_customize_register' );
      

      读入模板

      $my_theme_settings = get_option( 'my_theme_settings' );
      echo $my_theme_settings['social_facebook'];
      

      【讨论】:

        【解决方案4】:

        问题出在 WP_Customize_Setting::value() 它期望返回 false 取消选中复选框(或不选中复选框),而某些程序将返回 '0' 或 ''。

        在我的情况下,我必须扩展 WP_Customize_Setting 并覆盖 value() 方法以强制返回布尔值。

        <?php
        class ForceBooleanSettings
        extends WP_Customize_Setting {
        
          public function value() {
            return (bool) parent::value();
          }
        }
        
        // Example on using this extend class
        $customizer->add_setting(new ForceBooleanSettings(
           $customizer, 
           'myuniquekey', 
           array(
            'default' => false,
            'transport' => 'refresh',
        )));
        
        ?>
        

        【讨论】:

          【解决方案5】:

          这是我的工作解决方案:

          function theme_name_custom_settings( $wp_customize ) {
          
              $wp_customize->add_setting( 'social_facebook', array(
                  'default'   => 1, // Set default value
                  'sanitize_callback' => 'esc_attr', // Sanitize input
                  )
              );
          
              $wp_customize->add_control( 
                  new WP_Customize_Control(
                      $wp_customize,
                      'social_facebook', // Setting ID
                      array(
                          'label'     => __( 'Facebook', 'theme_name' ),
                          'section'   => 'social_icons', // No hyphen
                          'settings'  => 'social_facebook', // Setting ID
                          'type'      => 'checkbox',
                      )
                  )
              );
          
          }
          
          add_action( 'customize_register', 'theme_name_custom_settings' );
          

          然后像这样检查它的值(0 或 1):

          if ( !get_theme_mod( 'social_facebook' ) ) { // false
              return;
          }
          
          // or
          
          if ( get_theme_mod( 'social_facebook' ) == 1 ) { // true
              return;
          }
          

          【讨论】:

            【解决方案6】:
            <div class="facebook" <?php echo ( get_theme_mod( 'social_facebook' ) ) ? "style='display:none;'" : "" ?>>
            

            【讨论】:

            • 请用更多的信息和解释扩展你的答案,所以它不仅仅是一个无法解释的代码块。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-02
            • 1970-01-01
            • 2012-11-27
            • 2019-01-23
            • 1970-01-01
            相关资源
            最近更新 更多