• Resolved dfwgreg

    (@dfwgreg)


    Hi all,
    I found a way to add custom fields to an attached photo but I don’t know how to display the input from those fields. I have a custom field called ‘photocredits’ and want to display the data from that custom field in the image, just like a caption.

    I’m using this to display the image:

    <?php $thumb_id = get_post_thumbnail_id($post->id); $args = array(	'post_type' => 'attachment','post_status' => null,'post_parent' => $post->ID,'include'  => $thumb_id);
    		$thumb_images = get_posts($args);
    		foreach ($thumb_images as $thumb_image) {
    		if($thumb_image->[customfield]) echo '<h2><span>','Image: ' . $thumb_image->[customfield] . '</h2></span>';
    		}
    		?>

    and [customfield] is where data from the custom field is to be displayed.

    Any help would be greatly appreciated.

    Thanks,
    Gregory S.

Viewing 5 replies - 1 through 5 (of 5 total)
  • I’m not sure about using custom fields, but when you upload an image you have the option to add a caption.
    In the past I’ve used this code:

    <?php if(has_post_thumbnail()) {
    	the_post_thumbnail();
    	echo '<span class="caption">'. get_post(get_post_thumbnail_id())->post_excerpt .'</span>';
    	}

    The post_excerpt is what generates the content in the caption field.

    Thread Starter dfwgreg

    (@dfwgreg)

    I appreciate the help but it didn’t solve the problem. I’m using Bill Erickson guide for adding custom fields for image attachments

    http://www.billerickson.net/wordpress-add-custom-fields-media-gallery/

    It’s setup exactly as it is in the guide, I’m just stuck on how to get the custom field to be displayed on my site.

    – Gregory S.

    I wish you would have sent that link earlier. It makes more sense now.

    I see that Bill has provided an answer on his post. Hope that helps.

    Thread Starter dfwgreg

    (@dfwgreg)

    OK, I finally got it working but I need someone to verify that the code I have is correct. What I want it to do is check the custom field [photo_credits] for data, if it does then add the word ‘Image:’ before the text. I’ve done this before with the caption but I’m having problems trying to get it to work with the custom fields.

    Here’s what I’ve done so far:

    <?php
    		$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
    		foreach ( $attachments as $attachment_id => $attachment ) {
    		if get_post_meta($attachment_id, 'photo_credits', true); echo '<h2><span>','Image: ' . echo get_post_meta($attachment_id, 'photo_credits', true); . '</h2></span>';
    		} ?>

    If it helps, this code is based on the previous question – http://wordpress.org/support/topic/add-additional-text-to-photo-caption?replies=3

    Thanks for all the help so far,
    Gregory S.

    Worked for me. Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display data from custom field from attached image in post’ is closed to new replies.