最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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() );
发布评论

评论列表(0)

  1. 暂无评论