这就是你要找的。
/* Define the custom box */
// WP 3.0+
add_action( \'add_meta_boxes\', \'staff_details_metabox\' );
// backwards compatible
add_action( \'admin_init\', \'staff_details_metabox\', 1 );
/* Do something with the data entered */
add_action( \'save_post\', \'save_staff_details\' );
/**
* Adds a box to the main column on the Post edit screen
*
*/
function staff_details_metabox() {
add_meta_box( \'staff_details\', __( \'Staff Details\' ), \'staff_details_options\', \'post\', \'side\', \'high\' );
}
/**
* Prints the box content
*/
function staff_details_options( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), $post->post_type . \'_noncename\' ); ?>
<label for="_staff_title" class="selectit"><?php _e( \'Title\' ); ?>:</label> <input type="text" id="_staff_title" name="_staff_title" value="<?php echo get_post_meta( $post->ID, \'_staff_title\', true ); ?>" size="25" />
<label for="_staff_experience" class="selectit"><?php _e( \'Experience\' ); ?>:</label> <input type="text" id="_staff_experience" name="_staff_experience" value="<?php echo get_post_meta( $post->ID, \'_staff_experience\', true ); ?>" size="25" /><?php
}
/**
* When the post is saved, saves our custom data
*/
function save_staff_details( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// 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[$_POST[\'post_type\'] . \'_noncename\'], plugin_basename( __FILE__ ) ) )
return;
// Check permissions
if ( !current_user_can( \'edit_post\', $post_id ) )
return;
// OK, we\'re authenticated: we need to find and save the data
if( \'post\' == $_POST[\'post_type\'] ) {
if ( !current_user_can( \'edit_post\', $post_id ) ) {
return;
} else {
update_post_meta( $post_id, \'_staff_title\', $_POST[\'_staff_title\'] );
update_post_meta( $post_id, \'_staff_experience\', $_POST[\'_staff_experience\'] );
}
}
}
如果您想在循环中使用自定义元,可以这样做。
if ( get_post_meta( get_the_ID(), \'_staff_title\', true ) ) {
printf( __( \'Staff Title: %s\' ), get_post_meta( $post->ID, \'_staff_title\', true ) );
}
if ( get_post_meta( get_the_ID(), \'_staff_experience\', true ) ) {
printf( __( \'Staff Experience: %s\' ), get_post_meta( $post->ID, \'_staff_experience\', true ) );
}