我创建了一个文本字段类型,当我放置一些文本并点击发布时,它将显示在我的前端。但是,当我删除文本以清理前端并再次单击“发布”时,它将停留在Textarea字段和前端。有什么帮助吗?
Function.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 ) );发布于 2016-02-08 12:47:15
只需删除此条件即可。
if( !empty( $woocommerce_text_field ) )
当前,您正在检查文本字段是否为空,更新文本字段的内容。
当您删除上述条件时,不管您是否放置内容,它都会更新该字段。
发布于 2016-02-08 13:54:31
是的,你可以去掉这个条件。
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
}https://stackoverflow.com/questions/35269908
复制相似问题