$key = "key";
$meta_boxes = array(
"item-count" => array(
"name" => "item-count",
"type" => "itemcount",
"std" => "",
"title" => "How many items?",
"description" => "Choose item count",
"options" => array(
"0" => "None", "1" => "1", "2" => "2", "2" => "2", "3" => "3", "4" => "4"),
),
"item-color" => array(
"name" => "item-color",
"type" => "itemcolor",
"std" => "",
"title" => "Item Colors",
"description" => "Choose item colors",
"options" => array(
"black" => "Black", "white" => "White", "red" => "Red"),
),
);
function create_meta_box() {
global $key;
if ( function_exists(\'add_meta_box\') ) {
add_meta_box( \'item-div\', \'Item Infos\', \'display_meta_box\', \'post\', \'normal\', \'high\' );
}
}
function display_meta_box() {
global $post, $meta_boxes, $key;
wp_nonce_field( plugin_basename( __FILE__ ), $key . \'_wpnonce\', false, true );
foreach($meta_boxes as $meta_box) {
$data = get_post_meta($post->ID, $key, true);
switch ( $meta_box[\'type\'] ) {
case "itemcount":
echo \'<table style="margin-bottom: 20px;" class="widefat">\';
echo \'<thead><tr><th>\'.$meta_box[\'title\'].\'</th></tr></thead>\';
echo \'<tbody style="background:#f9f9f9;"><tr><td>\';
echo\'<select name="\'.$meta_box[\'name\'].\'[]" multiple size="3">\';
function new_in_array( $needle, $haystack ) { if (is_array($haystack)) { return in_array($needle, $haystack); } else { return false; } }
$getcountmeta = get_post_meta( $post->ID, \'key\', true );
$getcountmeta2 = $getcountmeta[\'item-count\']; ?>
<?php foreach ($meta_box[\'options\'] as $item_count_key => $item_count_option) { ?>
<option <?php if ( new_in_array($item_count_key,$getcountmeta2)) { ?> selected <?php } ?> value="<?php echo $item_count_key; ?>"><?php echo $item_count_option; ?></option>
<?php }
echo \'</select>\';
echo \'<p>\'.$meta_box[\'description\'].\'</p>\';
echo \'</td></tr></tbody></table></div>\';
break;
case "itemcolor":
echo \'<table style="margin-bottom: 20px;" class="widefat">\';
echo \'<thead><tr><th>\'.$meta_box[\'title\'].\'</th></tr></thead>\';
echo \'<tbody style="background:#f9f9f9;"><tr><td>\';
echo\'<select name="\'.$meta_box[\'name\'].\'[]" multiple size="3">\';
function new_in_array2( $needle, $haystack ) { if (is_array($haystack)) { return in_array($needle, $haystack); } else { return false; } }
$getcolormeta = get_post_meta( $post->ID, \'key\', true );
$getcolormeta2 = $getcolormeta[\'item-color\']; ?>
<?php foreach ($meta_box[\'options\'] as $item_color_key => $item_color_option) { ?>
<option <?php if ( new_in_array2($item_color_key,$getcolormeta2)) { ?> selected <?php } ?> value="<?php echo $item_color_key; ?>"><?php echo $item_color_option; ?></option>
<?php }
echo \'</select>\';
echo \'<p>\'.$meta_box[\'description\'].\'</p>\';
echo \'</td></tr></tbody></table></div>\';
break;
}
}
}
function save_meta_box( $post_id ) {
global $post, $meta_boxes, $key;
foreach($meta_boxes as $meta_box) {
$data[ $meta_box[ \'name\' ] ] = $_POST[ $meta_box[ \'name\' ] ];
}
if ( !wp_verify_nonce( $_POST[ $key . \'_wpnonce\' ], plugin_basename(__FILE__) ) )
return $post_id;
if ( !current_user_can( \'edit_post\', $post_id ))
return $post_id;
update_post_meta( $post_id, $key, $data );
}
我在上面粘贴的代码将“key”meta\\u键添加到数据库中,其meta\\u值为
a:2:{s:10:"item-count";a:2:{i:0;s:1:"1";i:1;s:1:"2";}s:10:"item-color";a:2:{i:0;s:5:"black";i:1;s:5:"white";}} //i chose counts 1 and 2, colors black and white
我想要实现的是,将这些值存储在单独的元键中,而不是唯一的“key”meta\\u键,例如
meta_key item_color
meta_value a:2:{i:0;s:5:"black";i:1;s:5:"white";}
我不知道怎么做。有什么建议吗?