0

I'm currently making a option in customiser to hide content using a checkbox, tick to show and unticked to hide.. was using it to apply the display:block and display:none on a id to get the effect but this doesnt seem to work properly. ive been over the codex but cant seem to find a answer. is there a way to assign values to the checkbox? below is my code: so by default the box should be ticked and showing the content. when unticked it should switch to display:none

    // WooCommerce Setting (toggle display - products on homepage)
$wp_customize->add_setting( 'woocommerce_productdisplay' , array(
'default'     => 'block',
'transport'   => 'refresh',
'sanitize_callback' => '',
) );

// WooCommerce Control (toggle display - products on homepage)
$wp_customize->add_control(
new WP_Customize_Control( $wp_customize, 'woocommerce_productdisplay', array(
'label'     => __( 'Display Products on Page', 'woocommerce_productdisplay'),
'section'   => 'woocommerce_section',
'type'      => 'checkbox',

'settings'  => 'woocommerce_productdisplay',
) ) );

// ================================================================================== //

}

add_action( 'customize_register', 'woocommerce_customize_register' );

function woocommerce_customize_css() {
?>
    <style>
        #woocommerce-container {display: <?php echo get_theme_mod('woocommerce_productdisplay', '');  ?>; }
    </style>
<?php
}

add_action( 'wp_head', 'woocommerce_customize_css');
1

Looks like you are missing some things from the settings array, namely type need to be theme_mod here and capability should be set too. You can try:

$wp_customize->add_setting( 'woocommerce_productdisplay' , array(
'type'        => 'theme_mod',
'capability'  => 'edit_theme_options',
'default'     => '1',
'transport'   => 'refresh',
'sanitize_callback' => '',
) );

Also note default for a checkbox should be '1' for ticked. Then you would use slightly different code for the output also with this in mind:

<style>
#woocommerce-container {display: <?php 
    $display = get_theme_mod('woocommerce_productdisplay', '1');
    if ($display) {echo 'block';} else {echo 'none';}
?>; }
</style>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.