我有一篇带有两个自定义元框的自定义帖子,我使用本教程添加了这两个元框:http://wptheming.com/2010/08/custom-metabox-for-post-type/
当我更新帖子时,它消失了。我的代码:
function my_create_post_types() {
// Set UI labels for Custom Post Type
$labels = array(
\'name\' => _x( \'Startups\', \'Post Type General Name\', \'twentythirteen\' ),
\'singular_name\' => _x( \'Startup\', \'Post Type Singular Name\', \'twentythirteen\' ),
\'menu_name\' => __( \'Startups\', \'twentythirteen\' ),
\'parent_item_colon\' => __( \'Padre Startup\', \'twentythirteen\' ),
\'all_items\' => __( \'Todas las Startups\', \'twentythirteen\' ),
\'view_item\' => __( \'Ver Startup\', \'twentythirteen\' ),
\'add_new_item\' => __( \'Añadir nueva Startup\', \'twentythirteen\' ),
\'add_new\' => __( \'Añadir nueva\', \'twentythirteen\' ),
\'edit_item\' => __( \'Editar Startup\', \'twentythirteen\' ),
\'update_item\' => __( \'Actualizar Startup\', \'twentythirteen\' ),
\'search_items\' => __( \'Buscar Startup\', \'twentythirteen\' ),
\'not_found\' => __( \'No encontrado\', \'twentythirteen\' ),
\'not_found_in_trash\' => __( \'No encontrado\', \'twentythirteen\' ),
);
// Set other options for Custom Post Type
$args = array(
\'label\' => __( \'Startups\', \'twentythirteen\' ),
\'description\' => __( \'Startup news and reviews\', \'twentythirteen\' ),
\'labels\' => $labels,
// Features this CPT supports in Post Editor
\'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', \'page-attributes\' ,\'post-formats\',\'tags\'),
// You can associate this CPT with a taxonomy or custom taxonomy.
\'taxonomies\' => array(\'post_tag\'),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 5,
\'register_meta_box_cb\' => \'add_url_metaboxes\',
\'menu_icon\' => \'http://cink-innovation.es/hub/wp-content/themes/hub/images/startups.png\',
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'page\',
);
$img_src = $retina === \'true\' ? aq_resize( get_post_meta( $post_id, \'krown_post_image\', true ), \'1340\', null ) : aq_resize( get_post_meta( $post_id, \'krown_post_image\', true ), \'680\', null );
// Registering your Custom Post Type
register_post_type( \'Startups\', $args);
// Set UI labels for Custom Post Type
$labels = array(
\'name\' => _x( \'Mentores\', \'Post Type General Name\', \'shadow\' ),
\'singular_name\' => _x( \'Mentores\', \'Post Type Singular Name\', \'shadow\' ),
\'menu_name\' => __( \'Mentores\', \'shadow\' ),
\'parent_item_colon\' => __( \'Padre Mentores\', \'shadow\' ),
\'all_items\' => __( \'Todas los Mentores\', \'shadow\' ),
\'view_item\' => __( \'Ver Mentores\', \'shadow\' ),
\'add_new_item\' => __( \'Añadir nuevo Mentor\', \'shadow\' ),
\'add_new\' => __( \'Añadir nuevo\', \'shadow\' ),
\'edit_item\' => __( \'Editar Mentor\', \'shadow\' ),
\'update_item\' => __( \'Actualizar Mentor\', \'shadow\' ),
\'search_items\' => __( \'Buscar Mentor\', \'shadow\' ),
\'not_found\' => __( \'No encontrado\', \'shadow\' ),
\'not_found_in_trash\' => __( \'No encontrado\', \'shadow\' ),
);
// Set other options for Custom Post Type
$args = array(
\'label\' => __( \'Mentores\', \'shadow\' ),
\'description\' => __( \'Mentores news and reviews\', \'shadow\' ),
\'labels\' => $labels,
// Features this CPT supports in Post Editor
\'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', \'page-attributes\' ,\'post-formats\',\'tags\'),
// You can associate this CPT with a taxonomy or custom taxonomy.
\'taxonomies\' => array(\'post_tag\'),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 6,
\'menu_icon\' => \'http://cink-innovation.es/hub/wp-content/themes/hub/images/mentores.png\',
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'page\',
);
$img_src = $retina === \'true\' ? aq_resize( get_post_meta( $post_id, \'krown_post_image\', true ), \'1340\', null ) : aq_resize( get_post_meta( $post_id, \'krown_post_image\', true ), \'680\', null );
// Registering your Custom Post Type
register_post_type( \'Mentores\', $args);
}
/* Hook into the \'init\' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( \'init\', \'my_create_post_types\' );
/*.........................CUSTOM FIELDS...................................*/
function add_url_metaboxes() {
add_meta_box(\'wpt_url_boxes\', \'Información Adicional\', \'wpt_url_boxes\', \'startups\', \'normal\', \'high\');
}
// Metaboxes
function wpt_url_boxes() {
global $post;
// Noncename needed to verify where the data originated
echo \'<input type="hidden" name="urlmeta_noncename" id="urltmeta_noncename" value="\' .
wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';
// Get the location data if its already been entered
$web = get_post_meta($post->ID, \'_web\', true);
$face = get_post_meta($post->ID, \'_face\', true);
// Echo out the field
echo \'<p>Url de la Startup:</p>\';
echo \'<input type="text" name="_web" value="\' . $web . \'" class="wideurl" />\';
echo \'<p>Dirección de Facebook:</p>\';
echo \'<input type="text" name="_face" value="\' . $face . \'" class="wideface" />\';
}
// Save the Metabox Data
function wpt_save_url_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'urlmeta_noncename\'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( \'Startups\' == $_POST[\'post_type\'] ) {
} else {
if ( !current_user_can( \'edit_post\', $post_id ) )
return;
}
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// OK, we\'re authenticated: we need to find and save the data
// We\'ll put it into an array to make it easier to loop though.
$url_meta[\'_web\'] = $_POST[\'_web\'];
$url_meta[\'_face\'] = $_POST[\'_face\'];
// Add values of $events_meta as custom fields
foreach ($url_meta as $key => $value) { // Cycle through the $events_meta array!
if( $post->post_type == \'startups\' ) return; // Don\'t store custom data twice
$value = implode(\',\', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn\'t have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action(\'save_post\', \'wpt_save_url_meta\', 1, 2); // save the custom fields
有什么想法吗?谢谢