Need the URL of a WordPress Thumbnail/Featured Image?

It’s actually quite easy. Just use this:

$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

Then where you may need to use it, just echo $url.

I typically use it when I need to set a background image for an element, especially those that need “background-size:cover”. Granted this does require placing that style inline, but it shouldn’t be an issue overall, and there is little way around that on the browser level, without using additional scripting which can increase page load and has the potential to fail. Just make sure that when placing the background image within an inline style, you have to specify all of the settings for that background image within that same inline style, or it will not take effect. Placing those items in the stylesheet will not be sufficient.

Happy WordPressing!

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>