How to get Post thumbnail url

i have one page, that could be showing post from category.
i’d using this code

     <div id="grid" class="grid-container" style="display: block;">
     <ul class="grid columns-2">
     <?php
     $args = array(
    'category' => 0,
    'numberposts' => 9,
    'post_type' => 'post',
    'post_status' => 'publish',
    'suppress_filters' => true );
     $recent_posts = wp_get_recent_posts($args);
     foreach( $recent_posts as $recent ){
     echo '<li><a href="' . get_permalink($recent["ID"]) 
     . '" title="'.$recent["post_title"].'" ><img class="aligncenter wp-image-80" src="" alt="'.$recent["post_title"].'"/></a>
     <h4>'.$recent["post_title"].'</h4></li> ';
            }
        ?>
    </ul>
</div>

and the problem is, i can’t showing the thumbnail.
and i’m trying to find how to get post thumbnail url and put in it

Related posts

3 comments

  1. get_the_post_thumbnail is NOT the correct answer since that function call provides you with something like this: <img src="#">, and not with some URL only.

    Well, take this as an example.

    For what I have understand you need to get the post thumbnail url only, not the full HTML img object, this is the way you can achieve that:

    $args =array('numberposts' => 1,'post_type' => 'post','order' => 'DESC', 'posts_per_page'  => 1);
    $data = query_posts($args);
    $something = NULL;
    for($i=0;$i<count($data);$i++){ 
        $something[$i]['id'] = $data[$i]->ID;
        $post_thumbnail_id = intval(get_post_thumbnail_id( $something[$i]['id'] ));
        $array_thumbnail = wp_get_attachment_image_src( $post_thumbnail_id,'medium');
        $something[$i]['image_url']=$array_thumbnail[0];
        echo $something[$i]['image_url'];
    }
    

    $args = Arguments for the query.

    $data = The query result set.

    $something = The array you are going to use to storage the url of the featured image of the set of posts you want to use (in this case is just one, as one of the query arguments said so).

    $something[$i][‘id’] = The id of each post you are using.

    $post_thumbnail_id = The id of the picture set as featured image in the current post within the media library.

    $array_thumbnail = The actual url of the image that you need, as you can see it means that you are getting the src value of the HTML img object currently set as featured image in the current post.

    $something[$i][‘image_url’] = That what you look for.

    – FUNCTIONS USED –

    get_post_thumbnail_id($post_id)

    wp_get_attachment_image_src($media_post_id,$size)

  2. Try the below snippet by passing the Post Id.

    get_the_post_thumbnail( $post_id );                   
    
    get_the_post_thumbnail( $post_id, 'thumbnail' );      // Thumbnail (Note: different to Post Thumbnail)
    get_the_post_thumbnail( $post_id, 'medium' );         // Medium resolution
    get_the_post_thumbnail( $post_id, 'large' );          // Large resolution
    get_the_post_thumbnail( $post_id, 'full' );           // Original resolution
    
    get_the_post_thumbnail( $post_id, array( 100, 100) ); // Other resolutions
    

    Refer URL:
    https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/

Comments are closed.