Changing Customizer Select To Checbox, But Retaining Classes

admin2025-06-02  1

On a custom theme I had made, I had some options like so:

$wp_customize->add_setting('swag_header_display_homepage', array(
        'default' => 'header-display-homepage-yes'
    ));
    $wp_customize->add_control('swag_header_display_homepage', array(
        'label' => 'Display Header On Homepage',
        'section' => 'swag_header_display_section',
        'type' => 'select'
        'choices' => array(
            'header-display-homepage-yes' => __('Yes'),
            'header-display-homepage-no' => __('No')
        )
    ));

The choices you see there end up being classes that get added to the header.

I tried changing it to a checkbox instead of dropdown selection by changing 'type' => 'select' to 'type' => 'checkbox'. Although, I know "choices" are for dropdown selections. How can I keep the classes when changing it to a checkbox.

I tried

'std' => array(
            'header-display-homepage-yes' => __('1'),
            'header-display-homepage-no' => __('0')
        )

Which I know is wrong on so many levels. How can I do it properly, or is it even possible?

On a custom theme I had made, I had some options like so:

$wp_customize->add_setting('swag_header_display_homepage', array(
        'default' => 'header-display-homepage-yes'
    ));
    $wp_customize->add_control('swag_header_display_homepage', array(
        'label' => 'Display Header On Homepage',
        'section' => 'swag_header_display_section',
        'type' => 'select'
        'choices' => array(
            'header-display-homepage-yes' => __('Yes'),
            'header-display-homepage-no' => __('No')
        )
    ));

The choices you see there end up being classes that get added to the header.

I tried changing it to a checkbox instead of dropdown selection by changing 'type' => 'select' to 'type' => 'checkbox'. Although, I know "choices" are for dropdown selections. How can I keep the classes when changing it to a checkbox.

I tried

'std' => array(
            'header-display-homepage-yes' => __('1'),
            'header-display-homepage-no' => __('0')
        )

Which I know is wrong on so many levels. How can I do it properly, or is it even possible?

Share Improve this question asked Mar 5, 2019 at 21:48 XarcellXarcell 1035 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Checkboxes would be the incorrect choice for a yes/no option, because you'd be able to select both options. You want radio buttons. Just change type to radio. Radio buttons also support choices, so you won't need to change that part.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748838324a314144.html

最新回复(0)