Support

Account

Home Forums General Issues Function to Return Image Caption from ID not Image Object? Reply To: Function to Return Image Caption from ID not Image Object?

  • Ok, after much gnashing of teeth and sighing I finally got something that worked. Immediately the problem is that wp_get_attachment_metadata is for something else. I wanted wp_get_attachment

    Function:

    function wp_get_attachment( $attachment_id ) {
    
    	$attachment = get_post( $attachment_id );
    	return array(
    		'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
    		'caption' => $attachment->post_excerpt,
    		'description' => $attachment->post_content,
    		'href' => get_permalink( $attachment->ID ),
    		'src' => $attachment->guid,
    		'title' => $attachment->post_title
    	);
    }

    Useage:

    <?php
    $attachment = wp_get_attachment( get_field( 'primary_image' ) );
    echo $attachment['caption'];
    ?>