admin管理员组

文章数量:1426783

I have this piece of code

  <?php

                $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 )));
                $i=1;
                while ( $loop->have_posts() ) : $loop->the_post();
                $image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_posts[0]['ID']), 'full' );

    $thumbnailImg=$image[0]?$image[0]:catch_that_image($recent_posts[0]);
?>

That I follow then with

<div 
            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!

I have this piece of code

  <?php

                $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 )));
                $i=1;
                while ( $loop->have_posts() ) : $loop->the_post();
                $image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_posts[0]['ID']), 'full' );

    $thumbnailImg=$image[0]?$image[0]:catch_that_image($recent_posts[0]);
?>

That I follow then with

<div 
            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!

Share Improve this question asked May 17, 2019 at 0:47 TionebTioneb 31 bronze badge 1
  • try adding wp_reset_postdata(); at the end of your loop. – rudtek Commented May 17, 2019 at 3:14
Add a comment  | 

1 Answer 1

Reset to default 0

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() );

本文标签: post thumbnailsIssue with wordpress thumnailssame thumnail displayed everywhere