Retrieve first/single image from post

I’m trying to retrieve the first image from all of my post in WP using the snippet below:

    $args = array( 'posts_per_page' => 8, 'offset' => 1 );
    $myposts = get_posts($args);
    foreach( $myposts as $post ) : setup_postdata( $post ); 
    <li class="read-more">
        <a href="<?php the_permalink();?>"><?php the_title();?></a>
        <?php $gallery = get_post_gallery_images( $post ); ?>
        <?php echo '<img src="'.$gallery[0].'">'; ?>

When I run the page, I do not get any error but, UNKNOWN is display when I view src. When I var_dump($gallery) array is empty.

How can I get the right image for each post?

Read more here: Retrieve first/single image from post

Leave a Reply

Your email address will not be published. Required fields are marked *