Woocommerce 自定义字段 - Select 字段 - 如何做一个空白占位符
Woocommerce custom fields - Select field - how to do a blank placeholder
我在 woocommerce 常规选项卡中的价格下方创建了一个自定义字段:
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div id="wc-after-price">';
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'Price Label', 'woocommerce' ),
'options' => array(
'blank' => __( 'Choose one or leave blank', 'woocommerce' ),
'Per 50 Words' => __( 'Price is per 50 words', 'woocommerce' ),
'Per 100 Words' => __( 'Price is per 100 words', 'woocommerce' ),
'Per Page' => __( 'Price is per page', 'woocommerce' )
)
)
);
echo '</div>';
}
我的保存功能如下:
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_select = $_POST['_select'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );
}
显示输出时一切正常,但我正在努力弄清楚如何不显示第一个选项 "blank"。
我的下拉列表中的第一个选项 'blank' => __( 'Choose one or leave blank', 'woocommerce' ),
基本上只是一个占位符。我的下拉选择不是强制性的,所以可以留空。
目前,如果我保持下拉菜单不变,它会在输出中显示 "blank"。
我怎样才能让这个选项只作为占位符而不显示任何输出?
您可以将 value
保留为空字符串 ''
而不是 blank
。
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div id="wc-after-price">';
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'Price Label', 'woocommerce' ),
'options' => array(
'' => __( 'Choose one or leave blank', 'woocommerce' ),
'Per 50 Words' => __( 'Price is per 50 words', 'woocommerce' ),
'Per 100 Words' => __( 'Price is per 100 words', 'woocommerce' ),
'Per Page' => __( 'Price is per page', 'woocommerce' )
)
)
);
echo '</div>';
}
我在 woocommerce 常规选项卡中的价格下方创建了一个自定义字段:
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div id="wc-after-price">';
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'Price Label', 'woocommerce' ),
'options' => array(
'blank' => __( 'Choose one or leave blank', 'woocommerce' ),
'Per 50 Words' => __( 'Price is per 50 words', 'woocommerce' ),
'Per 100 Words' => __( 'Price is per 100 words', 'woocommerce' ),
'Per Page' => __( 'Price is per page', 'woocommerce' )
)
)
);
echo '</div>';
}
我的保存功能如下:
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_select = $_POST['_select'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );
}
显示输出时一切正常,但我正在努力弄清楚如何不显示第一个选项 "blank"。
我的下拉列表中的第一个选项 'blank' => __( 'Choose one or leave blank', 'woocommerce' ),
基本上只是一个占位符。我的下拉选择不是强制性的,所以可以留空。
目前,如果我保持下拉菜单不变,它会在输出中显示 "blank"。
我怎样才能让这个选项只作为占位符而不显示任何输出?
您可以将 value
保留为空字符串 ''
而不是 blank
。
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div id="wc-after-price">';
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'Price Label', 'woocommerce' ),
'options' => array(
'' => __( 'Choose one or leave blank', 'woocommerce' ),
'Per 50 Words' => __( 'Price is per 50 words', 'woocommerce' ),
'Per 100 Words' => __( 'Price is per 100 words', 'woocommerce' ),
'Per Page' => __( 'Price is per page', 'woocommerce' )
)
)
);
echo '</div>';
}