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

use*_*005 5 wordpress checkbox customization

我无法弄清楚如何从 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',
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

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

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

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

小智 5

$sticky_mod = get_theme_mod( 'wca_header_section_sticky' ) == '1' ? 'sticky' : '';
Run Code Online (Sandbox Code Playgroud)

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


Mic*_*l S 2

尝试使用并自定义它(在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' );
Run Code Online (Sandbox Code Playgroud)

在模板中读取

$my_theme_settings = get_option( 'my_theme_settings' );
echo $my_theme_settings['social_facebook'];
Run Code Online (Sandbox Code Playgroud)