有時我們在設(shè)計wordpress模板時需要考慮是否有特色圖像,也就是常說的縮略圖,在分類頁上如果一些文章有縮略圖一些沒有那就有點(diǎn)參差不齊不美觀,有沒辦法設(shè)置如果沒有文章縮略圖則自動顯示默認(rèn)圖呢?可以的,發(fā)菜來介紹一下。在achive.php文件中,我們可以通過has_post_thumbnail()先判斷是否有特色圖,如果有就調(diào)用特色圖,如果沒有就調(diào)用默認(rèn)圖,如/images/default.jpg,代碼如下
<div class="list">
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<div class="item wow zoomIn">
<a href="<?php the_permalink(); ?>">
<img src="<?php if ( has_post_thumbnail() ): ?><?php the_post_thumbnail_url( 'full' ); ?>" alt="<?php the_title(); ?>" width="520" height="300">
<?php else: ?><?php echo '/images/default.jpg'; ?>" alt="<?php the_title(); ?>" width="520" height="300">
<?php endif; ?>
<strong><?php the_title(); ?></strong>
<p><?php the_excerpt(); ?></p>
</a>
</div>
<?php endwhile; ?>
<?php posts_nav_link(); ?>
<?php else : ?>
no products!
<?php endif; ?>
</div>
直接調(diào)用方法,可能引起空白:
<?php the_post_thumbnail();?>