UPDATED CODE 12/12/2011
下面是函数中的代码。php。我将输出代码放在我的事件页面中。到目前为止,还没有什么产出。
<?php
// Custom Meta Data for Events
$meta_box[\'event\'] = array(
\'id\' => \'event-meta-details\',
\'title\' => \'Event Information\',
\'context\' => \'normal\',
\'priority\' => \'high\',
\'fields\' => array(
array(
\'name\' => \'Start Date\',
\'desc\' => \'(Enter yyyy/mm/dd)\',
\'id\' => \'start_date\',
\'type\' => \'text\',
\'default\' => \'\'
),
array(
\'name\' => \'Venue\',
\'desc\' => \'Venue of Event\',
\'id\' => $prefix . \'event_venue\',
\'type\' => \'radio\',
\'options\' => array(
array(\'name\' => \'Spruce Gallery\', \'value\' => \'Spruce Gallery\'),
array(\'name\' => \'Pine Gallery\', \'value\' => \'Pine Gallery\'),
array(\'name\' => \'Oak Gallery\', \'value\' => \'Oak Gallery\')
)
)
)
);
///////////////
function get_related_event_data( $venue, $field = \'all\' ) {
$event_data = array(
\'Spruce Gallery\' => array(
\'address\' => \'Test Address 1\',
\'phone_no\' => \'344-333-2333\',
\'url\' => \'http://www.url1.com\'
),
\'Pine Gallery\' => array(
\'address\' => \'Test Address 2\',
\'phone_no\' => \'444-333-3333\',
\'url\' => \'http://www.url2.com\'
),
\'Oak Gallery\' => array(
\'address\' => \'Test Address 3\',
\'phone_no\' => \'555-666-6666\',
\'url\' => \'http://www.url3.com\'
),
);
if( !isset( $event_data[$venue] ) )
return;
// Optionally return all fields(note: you can\'t echo arrays, so be sure to loop over the data if pulling all fields
if( \'all\' == $field )
return $event_data[$venue];
if( !isset( $event_data[$venue][$field] ) )
return;
return $event_data[$venue][$field];
}
///////////////
这是我放在页面上的代码。我放了一个测试回声以确保
$venue
正在获取信息,但地址不显示。
<?php
$venue = get_post_meta( $post->ID, \'event_venue\', true );
echo $venue;
echo get_related_event_data( $venue, \'address\' ); ?>
旧职位。
下面是填充我的元框的代码。
// Custom Meta Data for Events
$meta_box[\'event\'] = array(
\'id\' => \'event-meta-details\',
\'title\' => \'Event Information\',
\'context\' => \'normal\',
\'priority\' => \'high\',
\'fields\' => array(
array(
\'name\' => \'Start Date\',
\'desc\' => \'(Enter yyyy/mm/dd)\',
\'id\' => \'start_date\',
\'type\' => \'text\',
\'default\' => \'\'
),
array(
\'name\' => \'Venue\',
\'desc\' => \'Venue of Event\',
\'id\' => $prefix . \'event_venue\',
\'type\' => \'radio\',
\'options\' => array(
array(\'name\' => \'Spruce Gallery\', \'value\' => \'Spruce Gallery\', \'venue_url\' => \'http://www.sprucegallery.com/\'),
array(\'name\' => \'Pine Gallery\', \'value\' => \'Pine Gallery\'),
array(\'name\' => \'Oak Gallery\', \'value\' => \'Oak Gallery\')
)
)
)
);
以下是保存信息的代码。
// 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\');
`我想在带有元框的活动CPT中保存和重用关于多个场馆的多个数据点。我有CPT和元框,可以保存我输入的活动地点以及时间和日期。目标是建立一个场馆下拉列表。选择其中一个场馆后,还将选择该场馆的信息-地址、URL、电话等。
我计划以数组的形式写出信息,但不知道如何做到这一点。每次我尝试都没用。
存储多个信息点而不必每次键入它们的最佳方式是什么?最坏的情况我会meta_key
手动输入每一位信息,但这似乎是毫无意义的数据输入。一定有更好的办法。
提前谢谢你。