I'm displaying a featured image (as a background) in a hero div, and using the caption to add some content from the Media Library automatically. I'd also like to use the image's description field and title, but can't find documentation on that. Here's what I have so far, which works as expected:

<div class="hero">
    <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
    <div class="hero-image" style="background-image: url('<?php echo $thumb['0'];?>')"></div>
    <div class="hero-content">
        <p class="caption"><?php get_the_post_thumbnail_caption(); ?><?php the_post_thumbnail_caption(); ?></p>

I'm not sure if I'm already getting that info, but not echoing it...or if I need to do more to get it from the database. Thanks in advance for your help!

1 Answer 1


this might work:

$post_thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));
if ($thumbnail_image && isset($thumbnail_image[0])) {
    $img_description = $thumbnail_image[0]->post_content;
    $img_caption = $thumbnail_image[0]->post_excerpt;
    $img_alt = get_post_meta($post_thumbnail_id , '_wp_attachment_image_alt', true);
    $img_title = $thumbnail_image[0]->post_title;
  • Thank you, Michael. This doesn't return anything at this point. I should have mentioned, but this is outside the Loop. Any thoughts on what might pull the data?
    – GodWords
    Feb 10, 2020 at 23:52
  • did you echo any of the variables to see them on the front?
    – Michael
    Feb 11, 2020 at 0:51
  • Thought for a second there that I was pretty smart. I echoed them, and now it works perfectly. Thanks for the info and your patience. Now I have a hero section that contains the Featured Image as a background, uses the image Title as a headline, uses the caption as a paragraph, and uses the description as a way to get a URL into a call-to-action button. All this stored in the Media Library with the hero's banner image. You're a lifesaver, Michael.
    – GodWords
    Feb 11, 2020 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.