我目前正在customiser中使用复选框隐藏内容,勾选显示,取消勾选隐藏。。使用它在id上应用display:block和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\');
最合适的回答,由SO网友:majick 整理而成
看起来您缺少设置数组中的一些内容,即type
必须是theme_mod
此处和capability
也应设置。您可以尝试:
$wp_customize->add_setting( \'woocommerce_productdisplay\' , array(
\'type\' => \'theme_mod\',
\'capability\' => \'edit_theme_options\',
\'default\' => \'1\',
\'transport\' => \'refresh\',
\'sanitize_callback\' => \'\',
) );
另请注意
default
对于勾选的复选框,应为“1”。然后,您将为输出使用略有不同的代码,同时也要记住这一点:
<style>
#woocommerce-container {display: <?php
$display = get_theme_mod(\'woocommerce_productdisplay\', \'1\');
if ($display) {echo \'block\';} else {echo \'none\';}
?>; }
</style>