简体   繁体   中英

wordpress — Get posts if they had a thumbnail

Who knows, how can i get only posts, what had a thumbnail picture? I didn't find anything in WP documentation. Please, help! Filters of get_posts() are not including thumbnail.

This should work:

<?php 
   $loop = new WP_Query( array('meta_key' => '_thumbnail_id', 'post_type' => 'post')); 
   while($loop->have_posts()) : $loop->the_post(); 
?>
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail(); ?>
<?php endwhile; ?>

That will return all posts that has a thumbnail (featured image) attached.

you can check the post have thumb or not using has_post_thumbnail( $post_id );

example :

<?php
while ( have_posts() ) : the_post();
if(has_post_thumbnail( the_ID )) {
 #code....
}
endwhile;
?>

Function Reference/has post thumbnail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM