Edit 12/15/2011 Added Radio Button print code
// Add meta boxes to admin panel only needs to be added once
add_action(\'admin_menu\', \'plib_add_box\');
//Add meta boxes to post types
function plib_add_box() {
global $meta_box;
foreach($meta_box as $post_type => $value) {
add_meta_box($value[\'id\'], $value[\'title\'], \'plib_format_box\', $post_type, $value[\'context\'], $value[\'priority\']);
}
}
//Format meta boxes
function plib_format_box() {
global $meta_box, $post;
// Use once for verification
echo \'<input type="hidden" name="plib_meta_box_nonce" value="\', wp_create_nonce(basename(__FILE__)), \'" />\';
echo \'<table class="form-table">\';
foreach ($meta_box[$post->post_type][\'fields\'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field[\'id\'], true);
echo \'<tr>\'.
\'<th style="width:20%"><label for="\'. $field[\'id\'] .\'">\'. $field[\'name\']. \'</label></th>\'.
\'<td>\';
switch ($field[\'type\']) {
case \'text\':
echo \'<input type="text" name="\'. $field[\'id\']. \'" id="\'. $field[\'id\'] .\'" value="\'. ($meta ? $meta : $field[\'default\']) . \'" size="30" style="width:30%" />\'. \' - \'. $field[\'desc\'];
break;
case \'textarea\':
echo \'<textarea name="\'. $field[\'id\']. \'" id="\'. $field[\'id\']. \'" cols="60" rows="4" style="width:97%">\'. ($meta ? $meta : $field[\'default\']) . \'</textarea>\'. \'<br />\'. $field[\'desc\'];
break;
case \'select\':
echo \'<select name="\'. $field[\'id\'] . \'" id="\'. $field[\'id\'] . \'">\';
foreach ($field[\'options\'] as $option) {
echo \'<option \'. ( $meta == $option ? \' selected="selected"\' : \'\' ) . \'>\'. $option . \'</option>\';
}
echo \'</select>\';
break;
case \'radio\':
foreach ($field[\'options\'] as $option) {
echo \'<input type="radio" name="\' . $field[\'id\'] . \'" value="\' . $option[\'value\'] . \'"\' . ( $meta == $option[\'value\'] ? \' checked="checked"\' : \'\' ) . \' />\' . $option[\'name\'];
}
break;
case \'checkbox\':
echo \'<input type="checkbox" name="\' . $field[\'id\'] . \'" id="\' . $field[\'id\'] . \'"\' . ( $meta ? \' checked="checked"\' : \'\' ) . \' />\';
break;
}
echo \'<td>\'.\'</tr>\';
}
echo \'</table>\';
}
// Save data from meta box
function plib_save_data($post_id) {
global $meta_box, $post;
//Verify nonce
if (!wp_verify_nonce($_POST[\'plib_meta_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;
}
foreach ($meta_box[$post->post_type][\'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);
}
}
}
add_action(\'save_post\', \'plib_save_data\');
End Edit on 12/15/2011
Edit #2 Showing Printr output
Array (
[_edit_last] => Array ( [0] => 1 ) [_edit_lock] => Array ( [0] => 1323951531:1 )
[start_date] => Array ( [0] => 2011/11/18 ) [end_date] => Array ( [0] => 2011/12/31 )
[opening_time] => Array ( [0] => 2011-11-21 18:00 )
[artist_talk_time] => Array ( [0] => 2011-11-22 17:00 )
[_thumbnail_id] => Array ( [0] => 130 )
[_start_month] => Array ( [0] => 01 )
[_start_day] => Array ( [0] => 77 )
[_start_hour] => Array ( [0] => 03 )
[_start_year] => Array ( [0] => 2011 )
[_start_minute] => Array ( [0] => 00 )
[_start_eventtimestamp] => Array ( [0] => 201101770300 )
[_end_month] => Array ( [0] => 12 )
[_end_day] => Array ( [0] => 10 )
[_end_hour] => Array ( [0] => 03 )
[_end_year] => Array ( [0] => 2011 )
[_end_minute] => Array ( [0] => 00 )
[_end_eventtimestamp] => Array ( [0] => 201112100300 )
[event_venue] => Array ( [0] => Pine Gallery )
[closing_time] => Array ( [0] => 2011-12-22 19:00 )
[lecture_time] => Array ( [0] => 2011-11-21 19:00 )
[panel_time] => Array ( [0] => 2011-11-24 14:00 )
[special_event_time] => Array ( [0] => 2011-11-28 19:00 )
[workshop_time] => Array ( [0] => 2011-11-28 13:00 ) )
End Edit #2
根据这个网站上的建议,我改变了
get_post_meta
到
get_post_custom
以减少查询数量。现在,我很难从单选按钮设置的数组中获取两个值。
目标是通过单击单选按钮来设置场馆,并让它设置在阵列中预设的场馆url。我希望能够echo
两个值。
这是我正在使用的代码。
<?php
/* creating meta boxes in functions.php */
$meta_box[\'event\'] = array(
\'id\' => \'event-meta-details\',
\'title\' => \'Event Information\',
\'context\' => \'normal\',
\'priority\' => \'high\',
\'fields\' => array(
array(
\'name\' => \'Workshop\',
\'desc\' => \'YYYY-MM-DD 00:00 24 hour clock\',
\'id\' => \'workshop_time\',
\'type\' => \'text\',
\'default\' => \'\'
),
array(
\'name\' => \'Venue\',
\'desc\' => \'Venue of Event\',
\'id\' => $prefix . \'event_venue\',
\'type\' => \'radio\',
\'options\' => array(
array(\'name\' => \'Pine Gallery\', \'value\' => \'Pine Gallery\', \'http://www.pinegallery.com\' ),
array(\'name\' => \'Spruce Gallery\', \'value\' => \'Spruce Gallery\'),
array(\'name\' => \'Oak Gallery\', \'value\' => \'Oak Gallery\')
)
),
array(
\'name\' => \'Custom Venue\',
\'desc\' => \'Enter Venue Name if Enter Text Below is Checked\',
\'id\' => \'custom_event_venue\',
\'type\' => \'text\',
\'default\' => \'\'
),
array(
\'name\' => \'Featured / Reccomended\',
\'desc\' => \'Recommended Event\',
\'id\' => \'featured_event\',
\'type\' => \'checkbox\',
\'default\' => \'\'
)
)
);
/* In the post trying to get both keys in the array*/
$event_custom_meta=get_post_custom($post->ID); // Get all the data
$event_event_venue = $event_custom_meta[\'event_venue\'][0];
$event_url = $event_custom_meta[\'event_venue\'][1];
echo $event_event_venue;
echo $event_url;
?>
谢谢你。