I have added a custom meta box with only 2 text fields using this tutorial:
http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/
I have successfully used it on another site with a custom post type.
I have literally copied the code from the tutorial to my new site and am able to enter in data and save it. But when I try to echo the data in the loop of the custom post type, nothing shows up. Here is the code from my functions.php and events.php page.
functions.php
// Add the Meta Box
function add_custom_meta_box() {
add_meta_box(
'custom_meta_box', // $id
'Custom Meta Box', // $title
'show_custom_meta_box', // $callback
'event', // $page
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box');
// Field Array
$prefix = 'custom_';
$custom_meta_fields = array(
array(
'label'=> 'Artist Name',
'desc' => 'The Artist Name',
'id' => $prefix.'artistname',
'type' => 'text'
),
array(
'label'=> 'Date and Location',
'desc' => 'Month XXXX City',
'id' => $prefix.'datelocation',
'type' => 'text'
)
);
// The Callback
function show_custom_meta_box() {
global $custom_meta_fields, $post;
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
// Begin the field table and loop
echo '<table class="form-table">';
foreach ($custom_meta_fields as $field) {
// get value of this field if it exists for this post
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
} //end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_custom_meta($post_id) {
global $custom_meta_fields;
// verify nonce
if (!wp_verify_nonce($_POST['custom_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;
}
// loop through fields and save the data
foreach ($custom_meta_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);
}
} // end foreach
}
add_action('save_post', 'save_custom_meta');
and here is the relevant code in events.php
<?php
$post_meta_data = get_post_custom($post->ID);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=1'.'&paged='.$paged . '&post_type=event&orderby=descending'); if(have_posts()) : ?><?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<article class="fimage">
<div id="coupleimages">
<?php the_post_thumbnail('home'); ?>
</div>
<div class="bottombar">
<span class="previousp ser"><?php previous_posts_link('« PREVIOUS EVENT'); ?></span><p class="info"><span>Photos by <?php echo $post_meta_data['custom_artistname'][0]; ?></span></p><p class="ser"><?php the_title(); ?></p> <p class="info"><?php echo $post_meta_data['custom_datelocation'][0]; ?></p><p class="info"><a href="#" class="prev2"></a><a href="#" class="next2"></a></p><span class="nextp ser"><?php next_posts_link('NEXT EVENT »'); ?></span>
</div>
</article>
<?php endwhile; endif; ?>
Basically grabbing the custom field data with
$post_meta_data = get_post_custom($post->ID);
and echoing it with
echo $post_meta_data['custom_artistname'][0];
This exact setup works on a similar site. Not sure what I am missing.
Arnav Joy answers:
try three things in events.php before
$post_meta_data = get_post_custom($post->ID);
:-
first :-
try put
<?php wp_reset_query();?>
at the top of the page
so what ever you were doing was correct but there was just a little problem that
you have to call post meta inside the loop not before it , so your final code will something like this
<?php get_header();
?>
<?php /*Template Name: Events*/ ?>
<div id="wrap" role="main">
<div id="container" class="gallery events">
<div class="fcontainer">
<span class="boxShadow"></span>
<span class="boxShadow boxShadowBottom"></span>
<section id="featuredimage" class="featuredimage3">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=1'.'&paged='.$paged . '&post_type=event&orderby=descending'); if(have_posts()) : ?><?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); $post_meta_data = get_post_custom($post->ID); ?>
<article class="fimage">
<div id="coupleimages">
<?php the_post_thumbnail('home'); ?>
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail('event', 'secondary-image'); endif; ?>
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail('event', 'third-image'); endif; ?>
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail('event', 'fourth-image'); endif; ?>
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail('event', 'fifth-image'); endif; ?>
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail('event', 'sixth-image'); endif; ?>
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail('event', 'seventh-image'); endif; ?>
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail('event', 'eighth-image'); endif; ?>
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail('event', 'ninth-image'); endif; ?>
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail('event', 'tenth-image'); endif; ?>
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail('event', 'eleventh-image'); endif; ?>
<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail('event', 'twelfth-image'); endif; ?>
</div>
<div class="bottombar">
<span class="previousp ser"><?php previous_posts_link('« PREVIOUS EVENT'); ?></span><p class="info"><span>Photos by <?php echo $post_meta_data['custom_artistname'][0]; ?></span></p><p class="ser"><?php the_title(); ?></p> <p class="info"><?php echo $post_meta_data['custom_datelocation'][0]; ?></p><p class="info"><a href="#" class="prev2"></a><a href="#" class="next2"></a></p><span class="nextp ser"><?php next_posts_link('NEXT EVENT »'); ?></span>
</div>
<?php echo var_dump($post_meta_data); ?>
</article>
<?php endwhile; endif; ?>
</section>
<?php get_footer(); ?>
Dan | gteh comments:
thanks for fixing it.
Daniel Yoen answers:
I think
$meta = get_post_meta($post->ID, 'field');
Dan | gteh comments:
That's for single custom fields. Mine are in an array.
I've used this code before from that tutorial, so I must have missed something
Luis Abarca answers:
Its more like
echo $post_meta_data['custom_artistname']['id'];
Can you show us the result of var_dump( $post_meta_data ) ??
Dan | gteh comments:
array(3) {
["_edit_last"]=>
array(1) {
[0]=>
string(1) "1"
}
["_edit_lock"]=>
array(1) {
[0]=>
string(12) "1341370305:1"
}
["_wp_page_template"]=>
array(1) {
[0]=>
string(10) "events.php"
}
}
Dan | gteh comments:
strange thing is I literally copied the tutorial and changed the post type. but it seems the data is not in the array? I used this on another site successfully just yesterday.