个性化阅读
专注于IT技术分析

WordPress中带有the_post_thumbnail的图像标题

当在主循环中的帖子上显示WordPress中的the_post_thumbnail()图像时, 有没有办法显示图像标题。

谢谢!感谢所有帮助。


#1


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

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

#2


从WordPress 4.6开始, 功能the_post_thumbnail_caption()已添加到核心(/wp-includes/post-thumbnail-template.php)。

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

Fatal error: Cannot redeclare the_post_thumbnail_caption()

#3


我想到了:

/************************************************************\
* 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;
  }
}

#4


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>';
}
赞(0)
未经允许不得转载:srcmini » WordPress中带有the_post_thumbnail的图像标题

评论 抢沙发

评论前必须登录!