使用WordPress中的_post_thumbnail的图片标题

时间:2011-06-06 07:20:36

标签: php wordpress wordpress-theming

当在主循环中的帖子上在WordPress中显示the_post_thumbnail()图像时,是否可以显示图像标题。

谢谢!感谢所有帮助。

4 个答案:

答案 0 :(得分:1)

这是一个更简单,更简短的代码:

<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_excerpt; ?>

答案 1 :(得分:1)

自WordPress 4.6起,函数the_post_thumbnail_caption()已添加到核心(/wp-includes/post-thumbnail-template.php)。

使用此处发布的代码会导致错误:

Fatal error: Cannot redeclare the_post_thumbnail_caption()

答案 2 :(得分:0)

我明白了:

/************************************************************\
* Fetch The Post Thumbnail Caption
\************************************************************/

function the_post_thumbnail_caption() {
  global $post;

  $thumbnail_id    = get_post_thumbnail_id($post->ID);
  $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));

  if ($thumbnail_image && isset($thumbnail_image[0])) {
    echo $thumbnail_image[0]->post_excerpt;
  }
}

答案 3 :(得分:0)

if(!function_exists('get_post_thumbnail_caption')) {
    function get_post_thumbnail_caption($post_id = null) {
        $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
        $thumbnail_id = get_post_thumbnail_id($post_id);
        if ($thumbnail = get_post($thumbnail_id))
            return $thumbnail->post_excerpt;
        return '';
    }
}

if(!function_exists('the_post_thumbnail_caption')) {
    function the_post_thumbnail_caption($post_id = null) {
        echo get_post_thumbnail_caption($post_id);
    }
}

if(has_post_thumbnail()) {
    the_post_thumbnail();
    the_post_thumbnail_caption();

    $caption = get_post_thumbnail_caption(123);
    if('' == $caption)
        echo '<div class="caption">'.$caption.'</div>';
}