How to get Featured Image URL

wordpress-snippets

WordPress made it pretty easy to use featured image by introducing “post-thumbnail” support. Simply add below line to your theme’s functions.php and you’ll get a “Featured Image” section on the post edit screen where you can select and set a featured image.

add_theme_support('post-thumbnails');

The usage is also just work of one piece of code.
Use the_post_thumbnail() function will output the image as an image HTML.

What if you want to only use the image url instead of the html? There is no direct and obvious function to do that.

But we have another function wp_get_attachment_image_src

This function returns an ordered array with values corresponding to the (0) url, (1) width, (2) height, and (3) scale of an image attachment (or an icon representing any attachment).

So in order to get the featured image url, we’ll have to do as following within the loop.

We first check if we have a featured image, if yes, then move on to get the image url $thumb_url.

Save

Author: Leo

My name is Leo who's behind this blog. I've been working with wordpress since 2010. I'll write about themes, plugins, tutorials and all about wordpress here. Hope you find it useful!

Leave a Reply

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