• Resolved dfwgreg

    (@dfwgreg)


    Hi everyone,

    I’m looking for a way to add additional text using a custom field of the image.

    I want to add the text ‘Image:’ to the photo caption only if the text field has been added.

    I’m using this as a code:

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

    I tried an if statement to check the get_post_meta and it didn’t work. I only want it to show up if text as been added in the custom field called [photo_credits] field.

    Thanks,
    – Gregory S.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter dfwgreg

    (@dfwgreg)

    Update: I think I found a solution but I need to find a way to check for data in the custom field. This is what I have now:

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

    Any help would be greatly appreciated.

    Thanks,
    Gregory Schultz

    Thread Starter dfwgreg

    (@dfwgreg)

    Got it:

    <?php
    		$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
    		foreach ( $attachments as $attachment_id => $attachment ) {
    		$credits = get_post_meta( $attachment_id, 'photo_credits', true );
    		if( !empty( $credits ) ) echo '<h2><span>','Image: ' . $credits . '</h2></span>';
    		} ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add additional text to photo caption from a custom field?’ is closed to new replies.