第四个论点add_meta_box()
是上下文,在这种情况下teams
, 不latest discussion
或next fixture
就像你在这里一样。此外,由于您希望这些元盒仅显示在您的CPT的新建和编辑屏幕上,因此最简单的方法是为您的特定CPT注册元盒回调,并取消对admin_init
. 将其添加到$args数组中,例如。\'register_meta_box_cb\' => \'prefix_teams_cpt_add_metaboxes\'
. 因此,您的代码的修改版本:
// Register Teams
add_action(\'init\', \'register_teams\');
function register_teams() {
$labels = array(
\'name\' => _x(\'Teams\', \'post type general name\'),
\'singular_name\' => _x(\'Team\', \'post type singular name\'),
\'add_new\' => _x(\'Add New\', \'team item\'),
\'add_new_item\' => __(\'Add New Team\'),
\'edit_item\' => __(\'Edit Team\'),
\'new_item\' => __(\'New Team\'),
\'view_item\' => __(\'View Team\'),
\'search_items\' => __(\'Search Teams\'),
\'not_found\' => __(\'Nothing found\'),
\'not_found_in_trash\' => __(\'Nothing found in Trash\'),
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => null,
\'menu_icon\' => get_stylesheet_directory_uri() . \'/images/menu-teams.png\',
\'supports\' => array( \'title\', \'editor\', \'thumbnail\' ),
\'has_archive\' => true,
\'register_meta_box_cb\' => \'prefix_teams_cpt_add_metaboxes\'
);
register_post_type( \'teams\' , $args );
}
// Add Metaboxes
function prefix_teams_cpt_add_metaboxes(){
add_meta_box("latest_discussion", "Latest Discussion", "prefix_latest_discussion_metabox", "teams", "side", "low");
add_meta_box("next_fixture", "Next Fixture", "prefix_next_fixture_metabox", "teams", "side", "low");
}
function prefix_latest_discussion_metabox(){
global $post;
$custom = get_post_custom($post->ID);
$latest_discussion = $custom["latest_discussion"][0];
?>
<label>Latest Discussion URL:</label><br /><br />
<input size="40" name="latest_discussion" value="<?php echo $latest_discussion; ?>" />
<?php
}
function prefix_next_fixture_metabox(){
global $post;
$custom = get_post_custom($post->ID);
$next_fixture = $custom["next_fixture"][0];
?>
<label>Next Fixture:</label><br /><br />
<input size="40" name="next_fixture" value="<?php echo $next_fixture; ?>" />
<?php
}
// Save Fields
add_action(\'save_post\', \'prefix_save_teams_details\');
function prefix_save_teams_details(){
global $post;
update_post_meta($post->ID, "latest_discussion", $_POST["latest_discussion"]);
update_post_meta($post->ID, "next_fixture", $_POST["next_fixture"]);
}
请注意,我已经更改了一些函数名称-请确保始终为函数添加前缀,如果名称真正描述了功能,则更容易跟踪诸如元盒创建函数之类的内容。