我在hero div中显示一个特色图像(作为背景),并使用标题自动添加媒体库中的一些内容。我还想使用图像的描述字段和标题,但找不到相关文档。以下是迄今为止我所掌握的信息,这些信息如预期般有效:
<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>
我不确定我是否已经得到了这些信息,但没有回应。。。或者如果我需要从数据库中获取更多信息。提前感谢您的帮助!
最合适的回答,由SO网友:Michael 整理而成
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;
}