Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm following this tutorial by Justin Tadlock on creating meta boxes. http://wp.smashingmagazine.com/2011/10/04/create-custom-post-meta-boxes-wordpress/

I can get a meta box with a single field to work without problem but I would like to create a meta box with several fields.

For example a meta box called "Staff Details" with 2 fields called "Title" and "Experience".

Does anyone know how I can do this please?

Thanks!

share|improve this question
    
Have you searched the tag archives in here? More then enough examples about that. –  kaiser Dec 5 '11 at 18:15
add comment

1 Answer

up vote 0 down vote accepted

This is what you are looking for.

/* 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'] );
      }
  } 

}

And if you want to use the custom meta in your loop you could do it like this.

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 ) );
}
share|improve this answer
    
Thanks Joshua, that's exactly what I needed to build on from! Much appreciated!! –  Johannes Dec 5 '11 at 21:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.