通过在回调的“选项”数组中放置两个数组,我在一个元字段中设置了两个选择框。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
}