如果没有缩略图,则忽略缩略图空间

时间:2020-02-18 02:29:12

标签: php wordpress

所以我有以下代码循环浏览我的帖子,并以列表格式显示帖子。这是我正在使用的代码:

<?php while (have_posts()) : the_post(); ?>
    <div class="one-sixth first"><?php the_post_thumbnail(); ?></div>
    <div class="five-sixths"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
<?php endwhile; ?>

这是带有缩略图的图像:
enter image description here

在这里没有帖子缩略图可用时,它只留下一个空白:
enter image description here

1 个答案:

答案 0 :(得分:1)

如果检查缩略图是否已显示,否则显示占位符图像。希望对您有帮助。

<?php
// Must be inside a loop.

if ( has_post_thumbnail() ) {
    the_post_thumbnail();
}
else {
    echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) 
        . '/images/placeholder.jpg" />';
}
?>