WooCommerce多个定制字段代码

时间:2018-08-20 作者:Navid M

我使用指南向product post设置中的general选项卡添加了多个自定义字段。

当我在字段中输入数据时,它会保存并显示在产品页面上,没有问题;问题是,当我删除它们时,从字段中清除的数据,但创建的div和其他HTML元素仍保留在产品页面上。

这是我在函数中使用的代码。我的主题文件夹中的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
and for saving also in 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 );

in the single-product.php for showing:

  <?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{} ?>

1 个回复
SO网友:Navid M

stackoverflow和stackexchange都不能回答我的问题。我不知道也许他们可以编辑我的写作听写!。总之,在php课程中搜索后,我的代码是这样的,并且工作正常:

<?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{} ?>

结束