首先,要添加元盒,正确的操作挂钩是add\\u meta\\u box(),尽管admin\\u init可以工作,但我会将您的代码更改为:
add_action( \'add_meta_boxes\', \'my_add_meta_boxes\' );
function my_add_meta_boxes() {
add_meta_box( \'football_Game_meta_box\', \'Football Game Details\', \'display_football_Game_meta_box\', \'football_Games\', \'normal\', \'high\' );
}
现在进入你的问题。您可以将列表存储在自定义数组中,并在数组成员上循环以构建“选择”下拉列表。这个数组可以在插件中硬编码,也可以从任何地方获取数据;例如,您可以使用
Wordpress options mechanism, 使用自定义数据库表,甚至创建
custom post type 存储每个团队的信息。让我们来看一个使用自定义帖子类型的示例(您也可以使用此自定义类型在站点中创建团队概要文件):
//create custom post type
add_action( \'init\', \'create_team_post_type\' );
function create_team_post_type() {
$labels = array(
\'name\' => __( \'Team\'),
\'singular_name\' => __( \'Team\' ),
\'menu_name\' => __( \'Teams\' ),
\'all_items\' => __( \'All teams\' ),
\'add_new\' => __( \'Add new\'),
\'add_new_item\' => __( \'Add new team\' ),
\'edit_item\' => __( \'Edit team\' ),
\'new_item\' => __( \'New team\' ),
\'view_item\' => __( \'View team\' ),
\'search_items\' => __( \'Search teams\' ),
\'not_found\' => __( \'No teams found\' ),
\'not_found_in_trash\' => __( \'No teams trashed\' ),
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $labels,
\'description\' => __(\'Teams profiles management.\'),
\'public\' => true,
\'show_ui\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\',\'thumbnail\',\'taxonomies\' ),
\'taxonomies\' => array(),
\'has_archive\' => true
);
register_post_type( \'teams\', $args );
}
add_action( \'add_meta_boxes\', \'my_add_meta_boxes\' );
function my_add_meta_boxes() {
add_meta_box( \'football_Game_meta_box\', \'Football Game Details\', \'display_football_Game_meta_box\', \'football_Games\', \'normal\', \'high\' );
}
function display_football_Game_meta_box($post){
//You have to add each teams as a Team post through Wordpress admin area
//Now we get them
$teams = get_posts(array (
\'posts_per_page\' => -1,
\'post_type\' => \'teams\'
));
$post_meta = get_post_custom($post->ID);
//Nonce for verification in the save hook
wp_nonce_field( plugin_basename( __FILE__ ), \'teams_noncename\' );
$current_home_team = \'\';
if( isset($post_meta[\'home_team\'][0] ) ) $current_home_team = $post_meta[\'home_team\'][0];
echo build_dropdown(\'home_team\',$teams,$current_home_team);
$current_guest_team = \'\';
if( isset($post_meta[\'guest_team\'][0] ) ) $current_guest_team = $post_meta[\'guest_team\'][0];
echo build_dropdown(\'guest_team\',$teams,$current_guest_team);
}
function build_dropdown($field, $options, $current){
$return = \'<div><label for="\'.$field.\'">\'.$field.\'</label>\';
$return .= \'<select name="\'.$field.\'">\';
foreach($options as $option){
$selected = ($current == $option->ID) ? (\' selected\') : (\'\');
$return .= \'<option value="\'.esc_attr($option->ID).\'"\'.$selected.\'>\'.$option->post_title.\'</option>\';
}
$return .= \'</select></div>\';
return $return;
}
add_action( \'save_post\', \'team_save_postdata\' );
function team_save_postdata($post_id) {
//First we need to check if the current user is authorised to do this action.
//Make your own checks. Just an example
if ( isset($_POST[\'post_type\']) && \'football_Games\' == $_POST[\'post_type\'] ) {
if ( !current_user_can( \'edit_post\', $post_id ) ) return;
}
//Secondly we need to check if the request come from a verify form
if ( !isset( $_POST[\'teams_noncename\'] ) || ! wp_verify_nonce( $_POST[\'teams_noncename\'], plugin_basename( __FILE__ ) ) )
return;
//Thirdly we can save the value to the database
if(isset($_POST[\'home_team\']) ):
update_post_meta($post_id, \'home_team\', sanitize_text_field( $_POST[\'home_team\'] ) );
else:
if (isset($post_id)) {
delete_post_meta($post_id, \'home_team\');
}
endif;
if(isset($_POST[\'guest_team\']) ):
update_post_meta($post_id, \'guest_team\', sanitize_text_field( $_POST[\'guest_team\'] ) );
else:
if (isset($post_id)) {
delete_post_meta($post_id, \'guest_team\');
}
endif;
}