Hi @alkorr,
One thing you can do is, just add this condition check when the looping of the posts starts:
<?php if( !has_post_thumbnail() ){?> //Just after the loop starts
—-POST CONTENT—-
<?php }?> //Just before the loop ends
This will only print the posts that will not have the featured images.
Thanks!
Thread Starter
Alkorr
(@alkorr)
Hi Shobhit, thank you, it works but there is a downside to this code: not all my posts have a featured image, so I have a first loop in which I show 2 posts containing a featured image. It works fine. Now when I use your code to show 2 other posts with no featured image, I have to query 4 posts to get 2 showing… I guess it’s because your code ‘skips’ the posts with the featured image to only show the ones without it. If I query 2 posts, only one is showing. And since I’m not a coder, I have no idea how to make the query work properly.
Thanks again for your help 😉
try with:
$args = array(
'meta_key' => '_thumbnail_id',
'meta_compare' => 'NOT EXISTS'
)
$no_featured_image = new WP_Query( $args );
while ( $no_featured_image->have_posts() ) : $no_featured_image->the_post();
ETC.....
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Thread Starter
Alkorr
(@alkorr)
Hi Michael, thanks for your help but it doesn’t work, the query breaks my page, it’s all blank now…
Here’s my code:
<?php $args = array(
'posts_per_page' => 2,
'post__not_in' => get_option( 'sticky_posts' ),
'meta_key' => '_thumbnail_id',
'meta_compare' => 'NOT EXISTS')
$no_featured_image = new WP_Query( $args );
while ( $no_featured_image->have_posts() ) : $no_featured_image->the_post();
?>
My text
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I don’t know what’s wrong… 🙁
-
This reply was modified 9 years, 6 months ago by
Alkorr.
-
This reply was modified 9 years, 6 months ago by
Alkorr.
Hello,
There’s a syntax error with your code. Try
<?php $args = array(
'posts_per_page' => 2,
'post__not_in' => get_option( 'sticky_posts' ),
'meta_key' => '_thumbnail_id',
'meta_compare' => 'NOT EXISTS'
);
$no_featured_image = new WP_Query( $args );
while ( $no_featured_image->have_posts() ) : $no_featured_image->the_post();
?>
My text
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I hope that helps.
Thread Starter
Alkorr
(@alkorr)
Hi, it works great, thank you both very much for your help! 🙂