我在这个问题上工作了很长时间,真的很困惑。以下是我的自定义帖子类型的所有代码:
/* Services Post Type */
function services_post_type() {
$labels = array(
\'name\' => _x( \'Services\', \'service\' ),
\'singular_name\' => _x( \'Service\', \'service\' ),
\'add_new\' => _x( \'Add New\', \'service\' ),
\'add_new_item\' => _x( \'Add New Service\', \'service\' ),
\'edit_item\' => _x( \'Edit Service\', \'service\' ),
\'new_item\' => _x( \'New Service\', \'service\' ),
\'view_item\' => _x( \'View Service\', \'service\' ),
\'search_items\' => _x( \'Search Services\', \'service\' ),
\'not_found\' => _x( \'No services found\', \'service\' ),
\'not_found_in_trash\' => _x( \'No services found in Trash\', \'service\' ),
\'parent_item_colon\' => _x( \'Parent Service:\', \'service\' ),
\'menu_name\' => _x( \'Services\', \'service\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => false,
\'supports\' => array( \'title\', \'editor\' ),
\'taxonomies\' => array( \'category\' ),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'has_archive\' => true,
\'query_var\' => true,
\'can_export\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\'
);
register_post_type( \'service\', $args );
}
add_action( \'init\', \'services_post_type\' );
/*
* Custom meta boxes
*/
add_action( \'add_meta_boxes\', \'create_meta_boxes\' );
function create_meta_boxes() {
add_meta_box( \'my-meta-box-id\', __(\'Service Price\'), \'meta_box_info\', \'service\', \'normal\', \'low\' );
}
// Create meta box: Service Price
function meta_box_info( $post ) {
$values = get_post_custom( $post->ID );
wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' );
?>
<?php $text = get_post_meta($post->ID, \'meta_box_info\', true); ?>
<p>Price: <input type="text" name="meta_box_info" id="meta_box_info" size="25" /></p>
<?php
}
// Save meta box: Service Price
function save_my_metadata($ID = false, $post = false)
{
if($post->post_type != \'service\')
return;
update_post_meta($ID, \'my_metadata\', $_POST[\'my_metadata\']);
}