无法保存具有多个选择的单个字段上的发布元数据

时间:2013-04-25 作者:mroncetwice

通过在回调的“选项”数组中放置两个数组,我在一个元字段中设置了两个选择框。HTML输出正常,但元数据未保存到数据库中。。。

这是一张meta box设置的图片(无论出于何种随机原因,我认为这会很有帮助):

。。。aaaaaaaaaaaaaaaaaaaaaaaaa这是我的代码:

add_action( \'add_meta_boxes\', \'event_infobox_add\' );                                                        
add_action( \'save_post\', \'event_infobox_save\' );
function event_infobox_add() {
add_meta_box( 
\'event_infobox\', // $id 
\'Event Date, Time, and Venue Info\', // $title 
\'event_infobox_call\', // $callback
\'events\', // $page
\'normal\', // $context 
\'high\' // $priority   
);
}

// Time options for dropdowns in the HTML
$times = array(  \'hour00\' => \'12AM\' ,\'hour01\' => \'1AM\' ,\'hour02\' => \'2AM\' ,\'hour03\' => \'3AM\' ,\'hour04\' => \'4AM\' ,\'hour05\' => \'5AM\' ,\'hour06\' => \'6AM\' ,\'hour07\' => \'7AM\' ,\'hour08\' => \'8AM\' ,\'hour09\' => \'9AM\' ,\'hour10\' => \'10AM\' ,\'hour11\' => \'11AM\' ,\'hour12\' => \'12PM\' ,\'hour13\' => \'1PM\' ,\'hour14\' => \'2PM\' ,\'hour15\' => \'3PM\' ,\'hour16\' => \'4PM\' ,\'hour17\' => \'5PM\' ,\'hour18\' => \'6PM\' ,\'hour19\' => \'7PM\' ,\'hour20\' => \'8PM\' ,\'hour21\' => \'9PM\' ,\'hour22\' => \'10PM\' ,\'hour23\' => \'11PM\' );

// Meta Box fields HTML
$event_infobox_fields = array(
array(
     \'label\' => \'Event Time Frame\'
    ,\'desc\' => \'Enter time frame of the event here\'
    ,\'id\' => \'event_timeframe\'  //values stored with this key
    ,\'class\' => \'\'
    ,\'type\' => \'select\'
    ,\'options\' => array(
        \'timestart\' => $times,
        \'timeend\' => $times
    )
)
);

function event_infobox_call( $post ) {
global $event_infobox_fields, $post;  
// Use nonce for verification  
wp_nonce_field( plugin_basename( __FILE__ ), \'event_infobox_nonce\' );
echo \'<input type="hidden" name="event_infobox_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';  

// Loop it up
echo \'<table class="form-table">\';

foreach ($event_infobox_fields as $field) {  
    $meta = get_post_meta($post->ID, $field[\'id\'], true); 
    echo \'<tr>
    <th>
        <label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label>
        <span class="meta-description">\'.$field[\'desc\'].\'</span>
    </th>
    <td>\';
    switch($field[\'type\']) {

        case \'select\':

        // Event Timeframe Select Dropdowns
        if ( $field[\'id\'] == \'event_timeframe\') {
            foreach ($field[\'options\'] as $key => $value ){
                echo $meta;
                $keyhead = str_replace(\'time\',\'\',$key);
                echo \'<span>\'.$keyhead.\'</span>
                            <select style="width:200px; display:block;" name="\'.$key.\'" id="\'.$key.\'">\';
                $total = count($field[\'options\'][$key])-1;
                for ($i=0; $i<=$total; $i++){
                    if($i<10) {
                        $i="0$i";
                    }
                    echo \'<option style="width:200px; display:block;" value="\'.$field[\'options\'][$key][\'hour\'.$i].\'"\', $meta == $field[\'options\'][$key][\'hour\'.$i] ? \'selected="selected"\' : \'\',\'>\'.$field[\'options\'][$key][\'hour\'.$i].\'</option>\';
                }
                echo \'</select>\';
            }
        }

        ////////////

        break;  

    } //end switch  
echo \'</td></tr>\';  
} // end foreach

echo \'</table>\'; // end table  
}

// $ave $ave $ave
function event_infobox_save( $post_id ) { 
global $event_infobox_fields;  

// verify nonce  
if (!wp_verify_nonce($_POST[\'event_infobox_box_nonce\'], basename(__FILE__)))  
        return $post_id;  
// check autosave  
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)  
        return $post_id;  
// check permissions  
if (\'page\' == $_POST[\'post_type\']) {  
        if (!current_user_can(\'edit_page\', $post_id))  
                return $post_id;  
        } elseif (!current_user_can(\'edit_post\', $post_id)) {  
                return $post_id;  
}    
// loop through fields and save the data  
foreach ($event_infobox_fields as $field) {  
    $old = get_post_meta($post_id, $field[\'id\'], true);
    $new = $_POST[$field[\'id\']]; 

    if ($new && $new != $old) {  
            update_post_meta($post_id, $field[\'id\'], $new); 
    }

    elseif (\'\' == $new && $old) {  
            delete_post_meta($post_id, $field[\'id\'], $old);  
    }  
} // end foreach  
}

1 个回复
最合适的回答,由SO网友:vancoder 整理而成

在保存代码中$field[\'id\']event_timeframe, 中不存在的$_POST, 所以你的选择永远不会保存。

你需要深入研究你的options 要访问的数组timestarttimeend:

// loop through fields and save the data  
    foreach ( $event_infobox_fields as $field ) {
        foreach ( $field[\'options\'] as $option_key => $option_value ) {
            $old = get_post_meta( $post_id, $option_key, true );
            $new = $_POST[$option_key];
            if ( $new && $new != $old ) {
                update_post_meta( $post_id, $option_key, $new );
            } elseif ( \'\' == $new && $old ) {
                delete_post_meta( $post_id, $option_key, $old );
            }
        } // end foreach  
    }
请记住,您还必须更新下拉代码中使用的元,以便预先填充这些元。

结束

相关推荐

将Metabox日期月份编号转换为单词

我使用带有日期(日、月和年)的自定义metabox。问题是,当我尝试将日期数字转换为日期字时,例如10是10月。我使用此代码:function eventposttype_get_the_month_abbr($month) { global $wp_locale; for ( $i = 1; $i < 13; $i = $i +1 ) { if ( $i == $month ) $monthabbr = $wp