我所做的就是为我的帖子创建一个简单的元框,调用现有的标记,然后将它们显示在“选择”下拉列表中,供编辑器选择。我觉得我离完成这件事太近了。。。但是当我保存帖子时,我选择的元值没有保存。为了完全保存我的数据,我是否需要重新考虑或调整某个步骤?
//meta box functions for adding the meta box and saving the data
add_action(\'add_meta_boxes\',\'nffa_meta_box_tags\');
function nffa_meta_box_tags() {
add_meta_box(\'nffa_meta\', \'Project Tag Status\',\'nffa_meta_box\',\'post\',\'side\',\'high\');
}
function nffa_meta_box($post) {
//GET TAGS
$args = array(
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hide_empty\' => 0,
\'taxonomy\' => \'post_tag\'
);
$categories=get_categories($args);
foreach($categories as $category) {
$tags[] = $category->name ;
}
$prefix = \'NFFA_meta_\';
$mtags = array(
\'name\' => \'tags\',
\'desc\' => \'Project Status:\',
\'subdesc\' => \'Please set the status of this project\',
\'id\' => $prefix . \'tags\',
\'type\' => \'select\',
\'options\' => $tags
);
print "<pre>";
print_r($mtags);
print "</pre>";
//retrieve the custom meta box values
$nffa_meta_value = get_post_meta($post->ID,$mtags[\'id\'], true);
//nonce for security
wp_nonce_field(plugin_basename(__FILE__),\'nffa_save_meta_box\');
//custom meta box form elements via select
echo \'<p>\'.$mtags[\'desc\'].\' <select name="\'. $mtags[\'id\'].\'" id="\'.$mtags[\'id\'].\'">\';
foreach ($mtags[\'options\'] as $vtag) {
echo \'<option\', $nffa_meta_value == $vtag ? \' selected="selected"\' : \'\', \'>\', $vtag, \'</option>\';
}
echo \'</select>\';
echo \'</p>\';
echo \'<p class="howto" id="new-tag-post_tag-desc">\'.$mtags[\'subdesc\'].\'</p>\';
}
//save our meta data
add_action (\'save_post\',\'nffa_save_meta_box_tags\');
function nffa_save_meta_box_tags($post_id) {
//process form data if $_POST is set
if (isset($_POST[$mtags[\'id\']])) {
//if auto saving skip saving our meta box data
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
return;
//check nonce for security
check_admin_referer(
plugin_basename(__FILE__),\'nffa_save_meta_box\');
//save the metabox data as post meta using the post ID as a unqiue prefix
update_post_meta($post_id, $mtags[\'id\'], santize_text_field($_POST[$mtags[\'id\']]));
}
}