如何获取Wordpress自定义复选框的值

如何获取Wordpress自定义复选框的值,wordpress,checkbox,customization,Wordpress,Checkbox,Customization,我不知道如何从WP customize manager中的复选框中获取值(无论是否选中) 这是functions.php中的代码: $wp_customize->add_setting('social_facebook', array( 'type' => 'option', )); $wp_customize->add_control( new WP_Customize_Control( $wp_customize,

我不知道如何从WP customize manager中的复选框中获取值(无论是否选中)

这是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',
        )
    )
);
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' );
这就是我试图获取值的方式:

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

.facebook{display:inline!重要;}

这些复选框的值为“”(空)或“1”,因此系统会注册对它们的检查。但是,我不知道如何通过get_theme_mod方法获得值。而且,它们没有任何名称值,因此我也无法通过通常的方式获取值

尝试使用并自定义此选项(已测试,在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',
        )
    )
);
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'];

如果未选中“我的主题设置[社交facebook]”复选框:

<?php if( get_theme_mod( 'my_theme_settings[social_facebook]' ) == '') { ?>

 //if setting is unchecked content here will be shown

<?php } // end if ?>

//如果未选中设置,将显示此处的内容

有关全文,请参阅:

问题在于WP\u Customize\u设置::value(),它希望返回false 在某些程序返回“0”或“0”时取消选中复选框(或保持复选框未选中)

在我的例子中,我必须扩展WP_Customize_设置并重写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',
)));

?>


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

以下是我的工作解决方案:

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),如下所示:


get_theme_mod()与set_theme_mod()一起工作而不是设置APISo,你建议我做什么?