我正在试图隐藏特征图像标题,使其无法显示。当前,它将显示build-the-div(但不会填充它,因为它是空的)。理想情况下,我想做的是,如果有人在标题中输入了文本,则只显示div(post_excerpt
) 媒体面板中的框。
这是我正在研究的代码:
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 \'<span>\'.$thumbnail_image[0]->post_excerpt.\'</span>\';
}
}
这就是我自己试图隐藏标题(如果标题为空)的原因:
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\'));
$thumbnail_caption = $thumbnail_image[0]->post_excerpt;
$thumbnail_set = isset($thumbnail_image[0]);
if (! empty($thumbnail_set)) {
if ($thumbnail_image && isset($thumbnail_image[0])) {
echo \'<div class="img-caption"><h5>\'.$thumbnail_caption.\'</h5></div>\';
} else return;
} else return;
}
这是我的页面模板中的代码:
<section class="entry-content">
<?php if ( has_post_thumbnail() ) : ?>
<figure class="featured-img alignright">
<?php the_post_thumbnail(\'med_size\'); ?>
<?php the_post_thumbnail_caption(); ?>
</figure><!-- .entry-page-image -->
<?php endif; ?>
<?php the_content(); ?>
</section>
当我删除特色图片上的标题时。它消失了,但div和emtpy仍在创建中
h5
要素
以下是两种状态的屏幕截图:
link to when a caption is left blank
here is a link when a caption is filled in
谢谢你的建议和帮助!