help getting featured image using get_posts

I am using a homepage sorter to arrange the posts on the front page of my site because of that, I am getting the posts in this manner so that I can call each post individually instead of in a foreach loop

    $args = array( 'numberposts' => -1,'category_name' => 'homepage');
$home_post = get_posts( $args );
$image = get_the_post_thumbnail($post->ID);
global $more;
$more = 0;

Then I can call each post this way

<?php if( isset( $home_post[1] ) ) { ?>
 <p><?php echo $home_post[1]->post_excerpt; ?></p>
 <?php } ?>

My problem is that get_posts has no way to get the post thumbnail outside of using a loop. I can’t use a loop because I need my post called in a switch statement. I can’t find a way to get the post thumbnail. Do you know of a way?

1 Answer
1

get_the_post_thumbnail() expects the post ID as first parameter. So you need a post object like … $home_post[1].

echo get_the_post_thumbnail( $home_post[1]->ID );

… should do what you need.

Leave a Comment