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.

Quick and dirty, I have meta boxes pertaining to concert information. (i.e. venue and location) and am trying to figure out how to properly display them in my post.

At the moment to display the meta-box data I have

<?php 
$venue_info = get_post_custom_values("venue_info");
    if (isset($venue_info[0])) {
        };
?>
<?php if( $venue_info[0] ) : ?>
    <?php echo $venue_info[0] ?>
<?php endif; ?>

I realize this is a very jerry-rig way to go about displaying the data. Which is why I'm hoping to learn the proper format. Thank you for your time.

This is what my function.php looks like incase you need it

<?php   
include('preset-library.php');

//I create an array called $meta_box and set the array key to the relevant post type
// If custom post type, change the 'post' variable, which I don't 
$meta_box['post'] = array(

    //This is the id applied to the meta box
    'id' => 'post-format-meta',  

    //This is the title that appears on the meta box container
    'title' => 'Additional Post Format Meta',    

    //This defines the part of the page where the edit screen section should be shown
    'context' => 'normal',    

    //This sets the priority within the context where the boxes should show
    'priority' => 'high',

    //Here we define all the fields we want in the meta box
    'fields' => array(
        array(
            'name' => 'Venue',
            'desc' => 'venue information',
            'id' => 'venue_info',
            'type' => 'text',
            'default' => ''
        ),
        array(
            'name' => 'Location',
            'desc' => 'Location of the Venue',
            'id' => 'location_info',
            'type' => 'text',
            'default' => ''
        )
    )
);
add_action('admin_menu', 'plib_add_box');
?>

and my include is here

//Add meta boxes to post types
function plib_add_box() {
    global $meta_box;

    foreach($meta_box as $post_type => $value) {
        add_meta_box($value['id'], $value['title'], 'plib_format_box', $post_type, $value['context'], $value['priority']);
    }
}
//Formatting
function plib_format_box() {
  global $meta_box, $post;

  // verification
  echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';

  echo '<table class="form-table">';

  foreach ($meta_box[$post->post_type]['fields'] as $field) {
      // get current post meta data
      $meta = get_post_meta($post->ID, $field['id'], true);

      echo '<tr>'.
              '<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
              '<td>';
      switch ($field['type']) {
          case 'text':
              echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%" />'. '<br />'. $field['desc'];
              break;
          case 'textarea':
              echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
              break;
          case 'select':
              echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
              foreach ($field['options'] as $option) {
                  echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
              }
              echo '</select>';
              break;
          case 'radio':
              foreach ($field['options'] as $option) {
                  echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
              }
              break;
          case 'checkbox':
              echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' />';
              break;
      }
      echo     '<td>'.'</tr>';
  }

  echo '</table>';

}
// Save data from meta box
function plib_save_data($post_id) {
    global $meta_box,  $post;

    //Verify
    if (!wp_verify_nonce($_POST['plib_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }

    //Check > autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    //Check > permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) {
            return $post_id;
        }
    } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }

    foreach ($meta_box[$post->post_type]['fields'] as $field) {
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];

        if ($new && $new != $old) {
            update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
        }
    }
}

add_action('save_post', 'plib_save_data');

?>

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

To check for meta key value then display:

if ( get_post_meta( $post->ID, 'venue_info', true ) ) : echo get_post_meta( $post->ID, 'venue_info', true )

endif;

via: The Codex

share|improve this answer
    
Thank you! This did just the trick. For about 30 minutes I had been frustrated thinking this piece of code would not work for me. Turns out, since I changed the ID from info to venue_info it emptied out the custom field I had previously filled and essentially I was hoping for a BLANK field to appear. I've over explained myself plenty, thank you for your time. (I would up if I had the rep) –  mmaximalist Jun 23 '11 at 7:13
add comment

For all my custom metabox needs I am using this script. Then to call the metadata, all I have to do is <?php echo get_post_meta(get_the_ID(), '$prefix_$meta', true); ?> (where $prefix_ is optional and $meta is the data from the specific metabox you are calling)

share|improve this answer
add comment

I see you're storing venue and location both under the "venue_info" key, how do you differentiate between the two, or is that not important? get_post_custom_values() returns an array because its intended use is when you expect more than one venue_info key associated with a particular post. if you just want a single value, use get_post_meta('venue_info').

share|improve this answer
    
Thank you for pointing that out. I did fix this earlier and should have updated it in the post. –  mmaximalist Jun 23 '11 at 6:58
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.