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

问题描述 投票:-1回答:1

我一直在撞墙试图保存自定义元框选择选项。由于我在下拉列表中有多个选项,因此我很难弄清楚我实际上要保存的值。我试图瞄准select name="XXX",但仍然没有运气。任何帮助将不胜感激。

这是我的代码,显示选择选项下拉列表:

<?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);
    ?>

这段代码是节省的:

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

任何帮助将不胜感激。

编辑:我在woocommerce产品屏幕中使用它 - 不是页面编辑或后期编辑或插件编辑屏幕。

这是我正在使用的完整代码的更详细的粘贴:

    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']);
        }
    }
php wordpress
1个回答
0
投票

试试这个。您将值存储在post_meta中。但是来自get_option。不要使用get_option来获取post meta值。

 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']);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.