get_post_meta for media attachments
-
i’m working on a site for an art gallery. on a custom post_type, i’m trying to grab all images attached to the page and display each image’s correlating data entered into their custom fields. (in this case, things like year, size and medium have been entered into every piece of art’s custom fields via the attachments’ media editor.)
here’s what i have:
<?php $args = array( 'numberposts' => -1, 'orderby' => 'menu_order', 'order'=> 'ASC', 'post_mime_type' => 'image', 'post_parent' => $post->ID, 'post_status' => null, 'post_type' => 'attachment' ); $attachments = get_posts($args); if($attachments){ ?> <div class="slider"> <?php foreach ($attachments as $attachment) { $wp_large_image = wp_get_attachment_image_src($attachment->ID,'large', false); $wp_medium_image = wp_get_attachment_image_src($attachment->ID,'medium', false); ?> <div> <p><a href="<?php echo htmlentities($wp_large_image[0]); ?>" title="<?php echo apply_filters('the_title', $attachment->post_title); ?>"><img src="<?php echo htmlentities($wp_medium_image[0]); ?>" /></a></p> </div> <?php } ?> </div>
this grabs two sizes of the images perfectly, for use in a slideshow. the next part is the bit that isn’t playing nicely:
<?php foreach ( $attachments as $attachment ) { $image_title = $attachment->post_title; $year = get_post_meta($attachment->ID, 'Year', true); $media = get_post_meta($attachment->ID, 'Media', true); $inches = get_post_meta($attachment->ID, 'Inches', true); $cm = get_post_meta($attachment->ID, 'Centimeters', true); $price = get_post_meta($attachment->ID, 'Retail', true); ?> <p><span class="title"><?php echo $image_title; ?></span><br /> <?php echo $year; ?><br /> <?php echo $media; ?><br /> <?php echo $inches; ?><br /> <?php echo $cm; ?><br /> <?php echo $price; ?></p> <?php } ?>
the only thing that shows up is $image_title. i’ve used the get_post_meta function successfully on other pages pulling from the same images. in that instance, it is on a page, not a custom post_type the args include a category_name… is that making a difference?
- The topic ‘get_post_meta for media attachments’ is closed to new replies.