Get Featured Image URL programmatically

Post thumbnails are pretty useful and pretty easy to use in WordPress. Simply add:

To a theme’s functions.php file and you’ll get a Featured Image module on the admin screen for posts which allows you to select one.

It is also very easy to output that image as an HTML <img>:

But what if you just need the URL? Say, you’re going to use it as a background-image on an element rather than a content image. Unfortunately there is no super easy/obvious function for that.

Within the loop, you’ll have to do:

Then $thumb_url will be that URL.

Also published on Medium.

Leave a Reply

Notify of