【发布时间】:2016-08-19 17:08:03
【问题描述】:
在 WordPress 的主循环中的帖子上显示 the_post_thumbnail() 图像时,有没有办法在可用的地方显示图像标题。
谢谢!感谢所有帮助。
【问题讨论】:
标签: php wordpress wordpress-theming
在 WordPress 的主循环中的帖子上显示 the_post_thumbnail() 图像时,有没有办法在可用的地方显示图像标题。
谢谢!感谢所有帮助。
【问题讨论】:
标签: php wordpress wordpress-theming
这是一个更简单、更简短的代码:
<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_excerpt; ?>
【讨论】:
从 WordPress 4.6 开始,函数 the_post_thumbnail_caption() 已添加到核心 (/wp-includes/post-thumbnail-template.php)。
使用这里贴的代码会报错:
Fatal error: Cannot redeclare the_post_thumbnail_caption()
【讨论】:
我想通了:
/************************************************************\
* 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;
}
}
【讨论】:
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>';
}
【讨论】: