I have this piece of code


                $loop = new WP_Query(array('post__not_in' => array($latestId),'post_type' => 'post', 'posts_per_page' => 9,'paged' => ( get_query_var('page') ? get_query_var('page') : 1 )));
                while ( $loop->have_posts() ) : $loop->the_post();
                $image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_posts[0]['ID']), 'full' );


That I follow then with

            class="post-card__thumb__img h-bg-zoom__img" 
            style="background-image: url('<?php echo $thumbnailImg; ?>');"></div>

But all I get is the thumbnail from the last written post displayed everywhere (rather than the thumbnail of each specific post).

Something is obviously wrong with my loop. Any help would be greatly appreciated. Thank you!

  • try adding wp_reset_postdata(); at the end of your loop. – rudtek May 17 '19 at 3:14

I'm guessing the problem is this: $recent_posts[0]['ID']; and I don't see $recent_posts being defined in your code, so what exactly is that $recent_posts and where/how did you define it?:

$image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_posts[0]['ID']), 'full' );

And you're inside The Loop, so you could just call get_post_thumbnail_id() without having to specify the post ID — or use get_the_ID() to get the current post's ID:

$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' ); // use this or below
//$image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );

And I think the $thumbnailImg=$image[0]?$image[0]:catch_that_image($recent_posts[0]); should also be:

$thumbnailImg=$image[0]?$image[0]:catch_that_image( get_the_ID() );
  • Thank you so much. That fixed it :) – Tioneb May 17 '19 at 16:06
  • You're welcome. :) – Sally CJ May 17 '19 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.