admin管理员组

文章数量:1123926

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">
        <h1>NEED IMAGE DESCRIPTION HERE</h1>
        <p class="caption"><?php get_the_post_thumbnail_caption(); ?><?php the_post_thumbnail_caption(); ?></p>
    </div>
</div>

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!

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">
        <h1>NEED IMAGE DESCRIPTION HERE</h1>
        <p class="caption"><?php get_the_post_thumbnail_caption(); ?><?php the_post_thumbnail_caption(); ?></p>
    </div>
</div>

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!

Share Improve this question asked Feb 10, 2020 at 0:13 GodWordsGodWords 537 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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;
}

本文标签: