get_template_part in for loop

Your problem is that the variable passed to setup_postdata() must be the global $post variable, like this:

// Reference global $post variable.
global $post;

// Get posts.
$posts = get_posts(array(
    'post_type' => 'post',
    'post_count' => 4
));  

// Set global post variable to first post.
$post = $posts[0];

// Setup post data.
setup_postdata( $post );

// Output template part.
get_template_part( 'template-parts/post-thumbnail' );

// Reset post data.
wp_reset_postdata();

Now normal template functions, like the_post_thumbnail() inside the template part will reference the correct post.

Leave a Comment