drslez4322 2017-12-28 14:18
浏览 61
已采纳

如何在WordPress自定义元框中保存选择选项

I've been banging my head against the wall trying to save a custom meta box select option. I am having a hard time figuring out what I actually target to save the value, since I have multiple options in the dropdown. I try to target the select name="XXX" but still no luck. Any help would be greatly appreciated.

Here is my code to show the select option dropdown:

<?php
    $accessory_product_args = [
        'posts_per_page' => -1,
            'tax_query' => [
                'relation' => 'AND',
                [
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    'terms' => 'accessories'
                ],
            ],
        'post_type' => 'product',
        'orderby' => 'title',
    ];
    $accessory_product = new WP_Query( $accessory_product_args ); ?>

    <select name="_accessories_product_one" id="_accessories_product_one" class="widefat">
        <?php while ( $accessory_product->have_posts() ) : $accessory_product->the_post();
            $title_one = get_the_title();
            $postid_one = get_the_ID(); ?>
            <option value="<?=$postid_one?>" <?php selected( '_accessories_product_one[select_field_0]', $postid_one); ?>>
                <?=$title_one?>
            </option>
        <?php endwhile; ?>
    </select>

    <?php
//this below is used for testing to see if i have saved the value or not:
    $dropdown_option = get_option( '_accessories_product_one' ); // Array
    $dropdown_value =  $dropdown_option ['select_field_0']; // Option value

    var_dump($dropdown_option);
    var_dump($dropdown_value);
    ?>

This code is the saving:

    <?php
if (isset($_POST['_accessories_product_one']) {
    update_post_meta( $post_id, '_accessories_product_one', $_POST['_accessories_product_one']);
    } ?>

Any help would be greatly appreciated.

EDIT: I am using this within the woocommerce product screen - not Page edit or Post edit or a plugin edit screen.

Here is a more verbose paste of the full code I am using:

    function custom_product_basic_load() {

    add_action( 'add_meta_boxes_product' , 'custom_product_basic_add_meta_boxes_product' );
    add_meta_box( 'custom_product_basic_metabox' , __( 'Product Layout' ) , 'custom_product_basic_metabox' , 'product' , 'normal' , 'high' );
    add_action( 'save_post' , 'custom_product_basic_save_post' , 10 , 3 );

}
add_action( 'load-post.php' , 'custom_product_basic_load' );
add_action( 'load-post-new.php' , 'custom_product_basic_load' );

function custom_product_basic_metabox( $post ) {?>

        <input type="hidden" name="product_type" value="simple" />

        <?php
        $accessory_product_args = [
            'posts_per_page' => -1,
                'tax_query' => [
                    'relation' => 'AND',
                    [
                        'taxonomy' => 'product_cat',
                        'field' => 'slug',
                        'terms' => 'accessories'
                    ],
                ],
            'post_type' => 'product',
            'orderby' => 'title',
        ];
        $accessory_product = new WP_Query( $accessory_product_args ); ?>

        <select name="_accessories_product_one" id="_accessories_product_one" class="widefat">
            <?php while ( $accessory_product->have_posts() ) : $accessory_product->the_post();
                $title_one = get_the_title();
                $postid_one = get_the_ID(); ?>
                <option value="<?=$postid_one?>" <?php selected( '_accessories_product_one[select_field_0]', $postid_one); ?>>
                    <?=$title_one?>
                </option>
            <?php endwhile; ?>
        </select>

        <?php
        $dropdown_option = get_option( '_accessories_product_one' ); // Array
        $dropdown_value =  $dropdown_option ['select_field_0']; // Option value

        var_dump($dropdown_option);
        var_dump($dropdown_value);
        ?>
    <?php }

    function custom_product_basic_save_post( $post_id ) {
    if (isset($_POST['_accessories_product_one'])) {

            update_post_meta( $post_id, '_accessories_product_one', $_POST['_accessories_product_one']);
        }
    }
  • 写回答

1条回答 默认 最新

  • dsklzerpx64815631 2017-12-29 05:26
    关注

    Try this. you are storing the value in post_meta. but getting from get_option. Do not use get_option to get the post meta value.

     function custom_product_basic_load() {
    
        add_action( 'add_meta_boxes_product' , 'custom_product_basic_add_meta_boxes_product' );
        add_meta_box( 'custom_product_basic_metabox' , __( 'Product Layout' ) , 'custom_product_basic_metabox' , 'product' , 'normal' , 'high' );
        add_action( 'save_post' , 'custom_product_basic_save_post' , 10 , 3 );
    
    }
    add_action( 'load-post.php' , 'custom_product_basic_load' );
    add_action( 'load-post-new.php' , 'custom_product_basic_load' );
    
    function custom_product_basic_metabox( $post ) {?>
    
        <input type="hidden" name="product_type" value="simple" />
    
        <?php
    
        echo $productLayout =  get_post_meta( $post->ID, '_accessories_product_one',true);
    
        $accessory_product_args = [
            'posts_per_page' => -1,
                'tax_query' => [
                    'relation' => 'AND',
                    [
                        'taxonomy' => 'product_cat',
                        'field' => 'slug',
                        'terms' => 'accessories'
                    ],
                ],
            'post_type' => 'product',
            'orderby' => 'title',
        ];
        $accessory_product = new WP_Query( $accessory_product_args ); ?>
    
        <select name="_accessories_product_one" id="_accessories_product_one" class="widefat">
            <?php while ( $accessory_product->have_posts() ) : $accessory_product->the_post();
                $title_one = get_the_title();
                $postid_one = get_the_ID(); ?>
                <option value="<?=$postid_one?>" <?php selected( $productLayout, $postid_one); ?>>
                    <?=$title_one?>
                </option>
            <?php endwhile; ?>
        </select>
        <?php 
    }
    
    function custom_product_basic_save_post( $post_id ) {
        if (isset($_POST['_accessories_product_one'])) {
    
            update_post_meta( $post_id, '_accessories_product_one', $_POST['_accessories_product_one']);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题