我创建了一个文本字段类型,当我放置一些文本并点击发布时,它显示在我的前端。但是,当我删除文本以清理前端并再次单击"发布"时,它将保留在文本区域字段和前端上。请帮忙吗?
功能.php
function woo_add_custom_general_fields() {
global $woocommerce, $post;
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'titre produit', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' )
)
);
function woo_add_custom_general_fields_save( $post_id ){
// textfield
$woocommerce_text_field = $_POST['_text_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field', esc_html( $woocommerce_text_field ) );
只需删除此条件即可。
if( !empty( $woocommerce_text_field ) )
当前,您正在检查文本字段是否不为空,请更新文本字段的内容。
当您删除上述条件时,无论您是否放置内容,它都会更新该字段。
是的,您可以删除该条件
function woo_add_custom_general_fields_save( $post_id ){
// textfield
$woocommerce_text_field = $_POST['_text_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field', esc_html( $woocommerce_text_field ) );
else
update_post_meta( $post_id, '_text_field', '' );
}
**or** you can check like this also before updating or displaying:
if ( get_post_meta( $post->ID, '_text_field', true ) != '' ) {
// your code
}