...
Tutorials, Wordpress

How to get post featured image url in wordpress?

25 February 2019

When using get_the_post_thumbnail(), the_post_thumbnail() or related functions, the ‘post-thumbnail’ image size is used by default, though a different size can be specified instead as needed and use a post featured image url in wordpress.

Pre-defined sizes:

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

Example :

$posts = get_posts( array( 'posts_per_page' => 5 ) );
foreach ( $posts as $_post ) {
    if ( has_post_thumbnail( $_post->ID ) ) {
        echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
        echo get_the_post_thumbnail( $_post->ID, 'thumbnail' );
        echo '</a>';
    }
}
Output here

WordPress post featured image

Leave a Comments