How to get attachment image caption, alt or description

I have spend some time trying to figure out how to display caption of Featured image below.
In caption I have saved Copyright data and I need to show it below every image.
There is no function for that and here is my solution:

$thumb_img = get_post( get_post_thumbnail_id() ); // Get post by ID
echo $thumb_img->post_excerpt; // Display Caption
echo $thumb_img->post_content; // Display Description

Images are save just like a regular post as a post type ‘attachment’, and data you enter in Media Library under caption are saved like standard WP Post Object, en example in my case is:

WP_Post Object
(
    [ID] => 32
    [post_author] => 1
    [post_date] => 2014-01-28 13:25:05
    [post_date_gmt] => 2014-01-28 13:25:05
    [post_content] => Description
    [post_title] => my-image
    [post_excerpt] => Some Caption
    [post_status] => inherit
    [comment_status] => open
    [ping_status] => open
    [post_password] =>
    [post_name] => my-image
    [to_ping] =>
    [pinged] =>
    [post_modified] => 2014-01-28 13:25:05
    [post_modified_gmt] => 2014-01-28 13:25:05
    [post_content_filtered] =>
    [post_parent] => 26
    [guid] => http://localhost/wp/wp-content/uploads/my-image.jpg
    [menu_order] => 0
    [post_type] => attachment
    [post_mime_type] => image/jpeg
    [comment_count] => 0
    [filter] => raw
)

To get ‘Alternative Text’, you would need to use other function get_post_meta($id).
In my case:

$thumb_img = get_post_meta( get_post_thumbnail_id() ); // Get post meta by ID
echo $thumb_img['_wp_attachment_image_alt']['0']; // Display Alt text

Which will return something like this:

Array
(
    [_wp_attached_file] => Array
        (
            [0] => 2014/01/my-image.jpg
        )

    [_wp_attachment_metadata] => Array
        (
            [0] => a:5:{s:5:"width";i:1440;s:6:"height";i:900;s:4:"file";s:57:"2014/01/my-image.jpg";s:5:"sizes";a:10:{s:9:"thumbnail";a:4:{s:4:"file";s:55:"my-image-98x98.jpg";s:5:"width";i:98;s:6:"height";i:98;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:57:"my-image-300x187.jpg";s:5:"width";i:300;s:6:"height";i:187;s:9:"mime-type";s:10:"image/jpeg";}s:5:"large";a:4:{s:4:"file";s:58:"my-image-1024x640.jpg";s:5:"width";i:1024;s:6:"height";i:640;s:9:"mime-type";s:10:"image/jpeg";}s:10:"fwc_slider";a:4:{s:4:"file";s:57:"my-image-440x275.jpg";s:5:"width";i:440;s:6:"height";i:275;s:9:"mime-type";s:10:"image/jpeg";}s:15:"fwc_slide_thumb";a:4:{s:4:"file";s:55:"my-image-80x44.jpg";s:5:"width";i:80;s:6:"height";i:44;s:9:"mime-type";s:10:"image/jpeg";}s:13:"stadium_thumb";a:4:{s:4:"file";s:55:"my-image-84x49.jpg";s:5:"width";i:84;s:6:"height";i:49;s:9:"mime-type";s:10:"image/jpeg";}s:22:"stadium_carousel_thumb";a:4:{s:4:"file";s:56:"my-image-120x70.jpg";s:5:"width";i:120;s:6:"height";i:70;s:9:"mime-type";s:10:"image/jpeg";}s:14:"wm_girls_thumb";a:4:{s:4:"file";s:57:"my-image-115x115.jpg";s:5:"width";i:115;s:6:"height";i:115;s:9:"mime-type";s:10:"image/jpeg";}s:10:"news_thumb";a:4:{s:4:"file";s:57:"my-image-220x110.jpg";s:5:"width";i:220;s:6:"height";i:110;s:9:"mime-type";s:10:"image/jpeg";}s:10:"city_thumb";a:4:{s:4:"file";s:56:"my-image-120x90.jpg";s:5:"width";i:120;s:6:"height";i:90;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:10:{s:8:"aperture";i:0;s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";i:0;s:9:"copyright";s:0:"";s:12:"focal_length";i:0;s:3:"iso";i:0;s:13:"shutter_speed";i:0;s:5:"title";s:0:"";}}
        )

    [_edit_lock] => Array
        (
            [0] => 1392886101:1
        )

    [_edit_last] => Array
        (
            [0] => 1
        )

    [_wp_attachment_image_alt] => Array
        (
            [0] => Alt Text
        )

)

18 Comments

  1. Here’s how I do it:

    function get_post_thumbnail_field( $field = ‘caption’, $post_id = NULL, $suppress_filters = FALSE ) {
    $attachment_id = get_post_thumbnail_id( $post_id );

    if ( $attachment_id ) {

    $data = wp_prepare_attachment_for_js( $attachment_id );
    $field = $data[$field];

    if ( $suppress_filters ) return $field;

    return apply_filters(‘get_post_thumbnail_field’, $field);
    }
    return NULL;
    }

    function the_post_thumbnail_field( $field = ‘caption’, $post_id = NULL, $suppress_filters = FALSE ) {
    echo get_post_thumbnail_field( $field, $post_id, $suppress_filters );
    }

    1. Thanks for share, will add it to snippet.
      I did not had to use it much, but when I did first time, I had trouble finding where is data stored.

  2. Hi, I am new to wordpress and php etc.
    This is extremly very nice post for me.
    Thanks a lot………………….. Bobz

  3. Hello Bobz,

    I just tried your code, and it’s works. I also have a question with your code but I am not programmer, how to show all attachment images alt from children.

    Thanks

  4. That’s cool, but i need to know how to add alt text:

    $attachment = array(
    ‘post_mime_type’ => $wp_filetype[‘type’],
    ‘post_title’ => $title,
    ‘post_excerpt’ => $title,
    ‘post_content’ => $title,
    ‘post_status’ => ‘inherit,
    // Tested:
    ‘_wp_attachment_image_alt’ => “wp image alt”,
    ‘attachment_image_alt’ => “a image alt”,
    ‘image_alt’ => “image alt”,
    ‘post_alt’ => “post alt”,
    );

Leave a Reply

Your email address will not be published. Required fields are marked *