我使用指南在产品发布设置的“常规”标签中添加了多个自定义字段。
当我在字段中输入数据时,它会毫无问题地保存并显示在产品页面上;
问题在于,当我擦除它们时,从字段中清除但创建的div和其他html元素的数据仍保留在产品页面上。
这是我使用的代码 functions.php 在我的主题文件夹中:
// Custom field Type
?> <p class="form-field custom_field_type">
<label for="custom_field_type"><?php echo __( 'ویژگی های کلیدی محصول', 'woocommerce' ); ?></label>
<span class="wrap">
<?php $custom_field_type = get_post_meta( $post->ID, '_custom_field_type', true ); ?>
<input placeholder="<?php _e( '', 'woocommerce' ); ?>" type="text" name="_field_one" value="<?php echo $custom_field_type[0]; ?>" step="any" min="0" />
<input placeholder="<?php _e( '', 'woocommerce' ); ?>" type="text" name="_field_two" value="<?php echo $custom_field_type[1]; ?>" step="any" min="0" />
<input placeholder="<?php _e( '', 'woocommerce' ); ?>" type="text" name="_field_three" value="<?php echo $custom_field_type[2]; ?>" step="any" min="0" />
<input placeholder="<?php _e( '', 'woocommerce' ); ?>" type="text" name="_field_four" value="<?php echo $custom_field_type[3]; ?>" step="any" min="0" />
<input placeholder="<?php _e( '', 'woocommerce' ); ?>" type="text" name="_field_five" value="<?php echo $custom_field_type[4]; ?>" step="any" min="0" />
<input placeholder="<?php _e( '', 'woocommerce' ); ?>" type="text" name="_field_six" value="<?php echo $custom_field_type[5]; ?>" step="any" min="0" />
</span>
<span class="description"><?php _e( 'شش ویژگی کلیدی محصول را بنویسید', 'woocommerce' ); ?></span> </p> <?php
,保存也可以在 functions.php 中进行:
// Custom Field
$custom_field_type = array(
esc_attr( $_POST['_field_one'] ), esc_attr( $_POST['_field_two'] ),
esc_attr( $_POST['_field_three'] ), esc_attr( $_POST['_field_four'] ),
esc_attr( $_POST['_field_five'] ), esc_attr( $_POST['_field_six'] )
);
update_post_meta( $post_id, '_custom_field_type', $custom_field_type );
在 single-product.php 中显示 <?php
if (!empty( $custom_field_type = get_post_meta($post->ID, '_custom_field_type', true))) {
echo '<div id="vijegiha">';
echo '<h3 id="vijegih3">ویژگی های کلیدی</h3>';
echo '<ul>';
foreach($custom_field_type as $vijegi)
if (!empty( $vijegi )) {
echo '<li>'.$vijegi.'</li>';}
echo '</ul>';
echo '</div>';}else{} ?>
答案 0 :(得分:0)
类似于$ custom_field_type的声音可能是一个空数组,因此您可以尝试以下操作:
<?php
if (!empty( $custom_field_type = get_post_meta($post->ID, '_custom_field_type', true))) {
if(count($custom_field_type) > 0){
echo '<div id="vijegiha">';
echo '<h3 id="vijegih3">ویژگی های کلیدی</h3>';
echo '<ul>';
foreach($custom_field_type as $vijegi)
if (!empty( $vijegi )) {
echo '<li>'.$vijegi.'</li>';}
echo '</ul>';
echo '</div>';
}
}else{} ?>
答案 1 :(得分:0)
I solved it:
<?php
$custom_field_type = get_post_meta($post->ID, '_custom_field_type', true);
$custom_field_type = array_filter($custom_field_type, 'strlen');
if (count($custom_field_type)) {
echo '<div id="vijegiha">';
echo '<h3 id="vijegih3">ویژگی های کلیدی</h3>';
echo '<ul>';
foreach($custom_field_type as $vijegi)
if (!empty( $vijegi )) {
echo '<li>'.$vijegi.'</li>';}
echo '</ul>';
echo '</div>';
}else{} ?>
now it works correctly.