post thumbnails - Issue with wordpress thumnails - same thumnail displayed everywhere

I have this piece of code <?php$loop = new WP_Query(array('post__not_in' => array($latestId),'post_

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745485864a4629764.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信