How to get Post thumbnail url

2019-08-06 19:15发布

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

标签: php wordpress
3条回答
我想做一个坏孩纸
2楼-- · 2019-08-06 20:01

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/

查看更多
劫难
3楼-- · 2019-08-06 20:05

Use get_the_post_thumbnail function.
In this case it is better use The Loop fundamentals of WordPress.
References:
https://codex.wordpress.org/The_Loop
https://codex.wordpress.org/The_Loop_in_Action

查看更多
姐就是有狂的资本
4楼-- · 2019-08-06 20:09

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)

查看更多
登录 后发表回答