首页 > 解决方案 > 如果没有缩略图,则忽略缩略图空间

问题描述

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

<?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; ?>

这是带有缩略图的图像:
在此处输入图像描述

这是没有可用的帖子缩略图时,它只留下一个空白区域:
在此处输入图像描述

标签: phpwordpress

解决方案


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

<?php
// Must be inside a loop.

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

推荐阅读