douhun8647 2015-03-21 11:30 采纳率: 0%
浏览 52
已采纳

Wordpress元数据下拉列表未保存

I have 2 custom post types named clients and casestudies. I'm trying to build a meta box on the clients post type that will have a drop down list featuring the titles of all posts from the casestudies post type. This will end up with a page displaying the featured image from the clients post type, then hyperlink off to the relevant casestudies post if a selection is made from the drop down list.

I have followed this tutorial to get a meta box put together: http://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336

This is the meta box code I have in my functions.php file:

add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
    add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'clients', 'side', 'default' );
}

function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
    ?>     
    <p>
        <label for="my_meta_box_select">Select which case study this logo will link to when it is clicked:<br /><br /></label>
        <select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">
            <option value="No case study">No case study</option>
                <?php
                $casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );
                $casestudiesloop = new WP_Query( $casestudies );
                while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();
                    ?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>
            <?php
                endwhile;
                    ?>
        </select>
    </p>
    <?php    
}

add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    if( !current_user_can( 'edit_post', $post_id ) ) return;

    if( isset( $_POST['my_meta_box_select'] ) )
        update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_POST['my_meta_box_select'] ) );
}

The meta box displays correctly on the correct post type, but when I update the post it won't save the data.

Thanks.

  • 写回答

1条回答 默认 最新

  • duanrong5927 2015-03-21 11:37
    关注

    You don't have nonce hidden field. Save function would return nothing.

    <input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />
    

    UPDATE:

    So your cd_meta_box_cb function would be

    <?php
        function cd_meta_box_cb( $post )
        {
            $values = get_post_custom( $post->ID );
            $selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
            ?>
            <p>
                <label for="my_meta_box_select">Select which case study this logo will link to when it is clicked:<br /><br /></label>
                <select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">
                    <option value="No case study">No case study</option>
                        <?php
                        $casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );
                        $casestudiesloop = new WP_Query( $casestudies );
                        while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();
                            ?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>
                    <?php
                        endwhile;
                            ?>
                </select>
            </p>
            <input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />
            <?php
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求