使用wordpress元数据插件并尝试将元数据保存为多维数组(我稍后会讨论的文本中)。
这是我的代码:
function wordpress_metabox() {
$event_adress = explode('|', get_post_meta($post->ID, '_event_adress', true));
foreach ($event_id as $key => $id){
echo '<div class="event">
<div class="ticket">
<input type="text" name="_event_adress[]" value="'.$event_adress[$key].'" />
<input type="text" name="_event_price[]" value="'.$event_price[$key].'" />
</div><!-- end .ticket -->
<div class="ticket">
<input type="text" name="_event_adress[]" value="'.$event_adress[$key].'" />
<input type="text" name="_event_price[]" value="'.$event_price[$key].'" />
</div><!-- end .ticket -->
<input type="button" class="button" id="add_ticket" value="+ Add ticket" />
</div><!-- end .event -->
<input type="button" class="button" id="add_event" value="+ Add Event" />';
}
}
//this is the save function
function wordpress_metabox_save($post_id, $post) {
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
$event_meta['_event_adress'] = $_POST['_event_adress'];
$event_meta['_event_id'] = $_POST['_event_id'];
// Add values of $event_meta as custom fields
foreach ($event_meta as $key => $value) { // Cycle through the $event_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode('|', (array)$value); // If $value is an array, separate with |
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
实际应该是一个foreach循环,但我还没有那么远,这就是为什么我写两次相同的div来说明。有人可以回答我如何在多维数组中保存每张票的地址和价格吗?我不确定是否可以输入例如_event_adress [] []将每个数据存储在嵌套数组中?或者我必须输入第一个键吗?