Get Featured Image URL

Need to set and use a featured image for some post?

Here we will describe featured images and how to get images URL in your code outside of a loop.

To use a featured image you need to add the following line to functions.php file of your theme.

// This theme uses post thumbnails

add_theme_support( 'post-thumbnails' );

By default WordPress themes support this so you don’t have to worry about.

Adding a featured image is very easy if you click “Set featured image”:

After this step your post will have the featured image set.

Thanksa featured image you can use this code.

$thumbnail = get_the_post_thumbnail( $post->ID, 'thumbnail' );

but this code will return the <img> tag. To get the URL you need to have this:

if (has_post_thumbnail( $post->ID ) ) {

$image = wp_get_attachment_image_src( 

get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); 

$thumbnailURL = $image[0]; 

}

Thanks.

tags: & category: -