サムネイルのないすべての投稿をスキップしたい。コードはまだ正しく動作しません。
実際には、スクリプトはサムネイルのない投稿を表示しません。これは良いことですが、ループ内ではサムネイルのない投稿も投稿としてカウントされます。
たとえば、wordpress データベースに 10 件の投稿があるとします。そのうちの5つを表示したい。ただし、サムネイルがある投稿のみ。
<ul>
<?php
$args = array( 'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$my_posts = get_posts( $args );
global $post;
foreach( $my_posts as $post ) : setup_postdata($post);
if ( !has_post_thumbnail() ) {
continue;
} else {
?>
<li>
<div class="clearfix" >
<div class="thumb"><?php the_post_thumbnail('post-image-big'); ?></div>
<a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
<p class="category"><?php the_category(', '); ?></p>
</div>
</li>
<?php } ?>
<?php endforeach; ?>
</ul>