我目前正在一个网站上工作,我需要为自定义帖子类型保存几种不同类型的元数据。
目前,我有一个文本输入,正在保存,没有问题,但正在努力获得一个选择字段来执行相同的操作。
这是输出选择字段的功能:
function kwi_department_input() {
// Define function to create meta box for team member department.
global $post;
echo \'<input type="hidden" name="teammeta_noncename" id="teammeta_noncename" value="\' . wp_create_nonce(plugin_basename(__FILE__)) . \'" />\';
$department = get_post_meta($post->ID, \'_department\', true); ?>
<select name="_department" id="_department">
<option value="Directors" <?php selected($department, \'Directors\'); ?>>Directors</option>
<option value="Finance / Admin" <?php selected($department, \'Finance / Admin\'); ?>>Finance / Admin</option>
<option value="Customer Service Team" <?php selected($department, \'Customer Service Team\'); ?>>Customer Service Team</option>
<option value="Commercial Team" <?php selected($department, \'Commercial Team\'); ?>>Commercial Team</option>
</select>
<?php }
这是保存所有数据的函数:
function kwi_save_team_meta($post_id) {
if( !wp_verify_nonce( $_POST[\'teammeta_noncename\'], plugin_basename(__FILE__) )) {
return $post_id;
}
if( !current_user_can( \'edit_post\', $post_id ) ) {
return $post_id;
}
$team_meta = array(
\'_department\' => $_POST[\'_department\'],
\'_languages\' => $_POST[\'_languages\'] // Languages is the text input that is saving with no issue.
);
foreach($team_meta as $key => $value) {
if(get_post_meta($post_id, $key, FALSE)) {
update_post_meta($post_id, $key, $value);
} else {
add_post_meta($post_id, $key, $value);
}
if(!$value) {
delete_post_meta($post_id, $key);
}
}
}
任何关于我做错了什么的指点都将不胜感激。