【问题标题】:Multiple Select value saving Issue多选价值节省问题
【发布时间】:2013-12-12 07:24:02
【问题描述】:

我制作元框,在此我选择多个选项

$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);

<select name="opt_meta_author" id="opt_meta_author" multiple="multiple">
    <?php
        $auth_args = array(
            'post_type' => 'author',
            'orderby' => 'title',
            'order' => 'ASC'
        );

        $authors = new WP_Query($auth_args);

        while($authors->have_posts()) :
            $authors->the_post();
    ?>
        <option value="<?php echo the_title() ?>">
            <?php echo the_title() ?>
        </option>
    <?php
        endwhile;
    ?>
</select>

当我选择多个选项时,它只保存一个选项,我想保存选定的选项

有什么建议

保存元值

$opt_meta_author = $_POST['opt_meta_author'];
update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author);

【问题讨论】:

    标签: php wordpress save meta


    【解决方案1】:
    $opt_meta_author = unserialize(get_post_meta($post->ID, 'opt_meta_author', true));
    
    <select name="opt_meta_author" id="opt_meta_author" multiple="multiple">
                    <?php
                        $auth_args = array(
                            'post_type' => 'author',
                            'orderby' => 'title',
                            'order' => 'ASC'
                        );
                    $authors = new WP_Query($auth_args);
    
                    while($authors->have_posts()) :
                        $authors->the_post();
                ?>
                        <option value="<?php echo the_title() ?>">
                            <?php echo the_title() ?>
                        </option>
                <?php
                    endwhile;
                ?>
                </select>
    



    要在存储时获取多个选定值,请执行以下操作:

    $opt_meta_author = serialize($_POST['opt_meta_author']);
    

    【讨论】:

    • 什么也没发生,它只给了我这个 - s:6:"Nadeem";
    • @deemi.exe 如果选择多个,那么它将作为数组传递给服务器,并且您必须在保存到数据库之前进行序列化。
    • 我这样做 $opt_meta_author = serialize($_POST['opt_meta_author']); update_post_meta($post->ID, 'opt_meta_author', $opt_meta_author);
    • 当我保存多个选择值时,它只给我一个值
    【解决方案2】:

    $_POST['opt_meta_author'])var dump 是什么?如果是数组,则使用implode将其转换为字符串并将字符串保存到数据库中

    【讨论】:

    • 这是一个字符串,你能告诉我如何保存到数据库中
    • 我猜update_post_meta( $post-&gt;ID, 'opt_meta_author', $opt_meta_author); 应该可以保存在数据库中
    • 非常感谢我使用 implode 及其工作,但我犯的一个错误是 和它与 implode() 一起工作正常
    猜你喜欢
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 2019-02-24
    相关资源
    最近更新 更多