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:
<?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?
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
The function returns an array of all sorts of useful information.
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.