我有一个自定义post类型的自定义metabox,用于记录我所在校园内的系统停机情况。metabox中的一个字段是一个多选框,其中列出了校园中的所有建筑。
当我保存帖子时,我能够检索前端的建筑阵列。但是,如果我返回编辑帖子,multiselect不会高亮显示以前选择的建筑列表。因此,如果我更新帖子,我必须记住还要重新突出显示受停电影响的建筑物。所有其他(非数组)字段成功保留其数据。
Adding the custom MetaBox:
function add_custom_meta_box() {
add_meta_box(\'system_outage\',\'System Outage\',\'show_system_outage\',\'outage\',\'normal\',\'high\');
}
add_action(\'add_meta_boxes\', \'add_custom_meta_box\');
Create Field Array:
$prefix = \'sysout_\';
$outage_meta_fields = array(
array(
\'label\' => \'Buildings Affected\',
\'desc\' => \'Select the buildings affected\',
\'id\' => $prefix.\'buildings\',
\'type\' => \'multiselect\',
\'options\' => array(
\'building1\' => array(
\'label\' => \'Building 1\',
\'value\' => \'building1\'
),
\'building2\' => array(
\'label\' => \'Building 2\',
\'value\' => \'building2\' //This continues for a while
)
)
);
);
The Callback:
function show_system_outage() {
global $outage_meta_fields, $post;
echo \'<input type="hidden" name="custom_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
echo \'<table class="form-table">\';
foreach ($outage_meta_fields as $field) {
$meta = get_post_meta($post->ID, $field[\'id\'], true);
echo \'<tr>\';
echo \'<th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>\';
echo \'<td>\';
switch($field[\'type\']) {
case \'multiselect\':
echo \'<select data-placeholder="Choose a building..." multiple="true" class="chosen" name="\'.$field[\'id\'].\'[]" id="\'.$field[\'id\'].\'">\';
foreach ($field[\'options\'] as $option) {
echo \'<option\', $meta == $option[\'value\'] ? \' selected="selected"\' : \'\', \' value="\'.$option[\'value\'].\'">\'.$option[\'label\'].\'</option>\';
}
echo \'</select><br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
}
echo \'</td>\';
echo \'</tr>\';
}
echo \'</table>\';
}
And Then I Save the Data:
function save_custom_meta($post_id) {
global $outage_meta_fields;
if (!wp_verify_nonce($_POST[\'custom_meta_box_nonce\'], basename(__FILE__)))
return $post_id;
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
return $post_id;
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 ($outage_meta_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\', \'save_custom_meta\');
我希望我已经把它格式化了,这样它就可以模糊地阅读了。对此,我们将不胜感激。谢谢