Title and Caption of Featured Image not being shown in Image Meta on WordPress

I have added a featured image to a WordPress page, and with that I have added a Title and Caption to the image.

Then I have used the following code to access the image:

Read More
<?php if (has_post_thumbnail( $post->ID ) ): ?>
  <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
  <?php $meta = wp_get_attachment_metadata( get_post_thumbnail_id( $post->ID), 'single-post-thumbnail' ); ?>

  <?php print_r($meta); ?>
    <div class="full_page_photo" style="background-image: url('<?php echo $image[0]; ?>');">
<?php endif; ?>

The <?php print_r($meta); ?> tag is for testing only, and the test has returned the following information contained within that variable:

  Array
(
    [width] => 1920
    [height] => 480
    [file] => 2015/01/01.png
    [sizes] => Array
        (
            [thumbnail] => Array
                (
                    [file] => 01-150x150.png
                    [width] => 150
                    [height] => 150
                    [mime-type] => image/png
                )

            [medium] => Array
                (
                    [file] => 01-300x75.png
                    [width] => 300
                    [height] => 75
                    [mime-type] => image/png
                )

            [large] => Array
                (
                    [file] => 01-1024x256.png
                    [width] => 1024
                    [height] => 256
                    [mime-type] => image/png
                )

            [post-thumbnail] => Array
                (
                    [file] => 01-624x156.png
                    [width] => 624
                    [height] => 156
                    [mime-type] => image/png
                )

        )

    [image_meta] => Array
        (
            [aperture] => 0
            [credit] => 
            [camera] => 
             => 
            [created_timestamp] => 0
            [copyright] => 
            [focal_length] => 0
            [iso] => 0
            [shutter_speed] => 0
            [title] => 
            [orientation] => 0
        )

)

For some reason, even though I have provided the image with a Title and Caption these fields are returned as blank. On editing the page, the image displays the Title and Caption I provided to it, so the information is being saved to the system.

Why is it returning blank fields?

Related posts

Leave a Reply

2 comments

  1. I was also looking for this information. I found the answer in WordPress: Get description, caption, alt, file URL, from an image ID without the image being a post thumbnail

    Try wp_prepare_attachment_for_js (http://codex.wordpress.org/Function_Reference/wp_prepare_attachment_for_js )

    I have yet to use it, but I know its supposed to grab all the information from >an attachment including title, description, alt, caption, etc.

    The function returns an array of all sorts of useful information.

  2. I encountered the same problem that the caption nor alt wasn’t showing. After some searching I found a usable function to display the data of an image. So if anyone else is still looking here it is.

    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
        );
    }