我一直在为我正在制作的主题添加选项。我在选择框中有多个选项时遇到了麻烦。我期待着echo
代码,取决于选项字段中保存的值。
选项
function nikon_woocommerce_shop_width() {
$shop_width = esc_attr( get_option( \'nikon_woocommerce_shop_width\' ) );
?>
<select name="nikon_woocommerce_shop_width" value="<?php $nikon_woocommerce_shop_width; ?>">
<option value="1" <?php selected( $shop_width, 1 ); ?> >1/4</option>
<option value="2" <?php selected( $shop_width, 2 ); ?> >1/3</option>
<option value="3" <?php selected( $shop_width, 3 ); ?> >1/2</option>
<option value="4" <?php selected( $shop_width, 4 ); ?> >Full</option>
</select>
<?php
}
功能
$woocomemrce_shop_width = esc_attr( get_option( \'nikon_woocomemrce_shop_width\' ) );
function nikon_woocommerce_shop_width_preoutput() {
if( selected( 1, $woocommerce_shop_width, false ) ) {
echo \'class="col-3\';
} else if( selected( 2, $woocommerce_shop_width, false ) ) {
echo \'class="col-4\';
} else if( selected( 3, $woocommerce_shop_width, false ) ) {
echo \'class="col-6\';
} else if( selected( 4, $woocommerce_shop_width, false ) ) {
echo \'class="col-12\';
}
}
add_action( \'nikon_woocommerce_shop_width_output\', \'nikon_woocommerce_shop_width_preoutput\' );
前端HTML
<?php
$woocommerce_shop_class = esc_attr( get_option( \'nikon_woocommerce_shop_class\' ) );
$woocomemrce_shop_width = esc_attr( get_option( \'nikon_woocommerce_shop_width\' ) );
?>
<div id="woocommerce_shop">
<?php if( selected( 2, $woocommerce_shop_class, false) ) { echo \'\'; } else { echo \'<div class="container">\'; } ?>
<div class="row">
<div <?php echo $woocomemrce_shop_width; ?> <?php do_action( \'nikon_woocommerce_shop_width_output\' ); ?> >
<?php woocommerce_content(); ?>
</div>
<div class="col">
<?php get_sidebar( \'woocommerce\' ); ?>
</div>
</div>
<?php if( selected( 2, $woocommerce_shop_class, false) ) { echo \'\'; } else { echo \'</div>\'; } ?>
</div>
我可以
echo
仅显示选项,它将显示与已保存选项相关的编号,但我似乎无法确定如何使
if
报表工作。
如果您需要更多详细信息,请让我知道!