WordPress Managed Hosting - 40% Off For 4 Months - Coupon Code: BFCM2021 Avail Now

How to get WordPress post featured image URL & Post Thumbnail

Owais Alam — November 20, 2019 3 Minutes Read

In many cases, developers need the direct URL of a featured image. This is because the featured image of a post, also known as a post thumbnail, is used in individual posts, pages, and custom posts and plays a vital role in WordPress. It’s just like other media files that help grab the attention of the visitors, but the featured image is the most important of them all. 

When your visitors land on your WordPress blog, the featured image acts as a visual cue to what the article is about and makes the post more interesting. In fact, when you share the blog on social media, it acts as a banner as well.

Let’s find out how to get WordPress post featured image URL and post thumbnail.

How to Get WordPress Post Featured Image URL

You can display the featured images in your custom theme by adding the following code in templete.php:

In many cases, though, developers need the direct URL of a featured image and want to use and display it in different ways. For example, when the featured image is used in a header, in an archive page, in a recent post page, or as the post background image, etc. 

In these cases, the requirement is more than just displaying the featured image in the post. You can use the featured image URL to fulfill any requirements related to the above scenarios. 

Also Read: How to Add WordPress Favicon to Your WordPress Website

First, I defined a wpblog-featured-image class in the CSS file in order to style the featured image. You can choose your own styling formats for this class but I would suggest that you use the following CSS style because it’s one of the most recommended styles by experts:

For the post ID (for $post->ID), you can check the URL of your post. As shown in the image below, my post ID is 12.

Get Featured Image URL and Post Thumbnail

Get Featured Image URL

To get featured image URL, place the following code in the file single.php, post.php and template.php:

Also Read: How to Change PHP Version in Xampp

The get_post_thumbnail_id is used to retrieve the post thumbnail ID.

The wp_get_attachment_url returns a full URI for an attachment file or false on failure.

Get Single Post Thumbnail

In many cases, the post thumbnail is the feature of the theme and not part of the core WordPress capabilities. To enable support for the featured image on post and pages, use the following snippet:

The wp_get_attachment_image_src returns the value of the image attribute that contains different values: URL, width, height, and size of the image.

While you are at it, don’t forget to optimize your images using these best WordPress image compression plugins.

Conclusion

So there you have it. I hope you’ve now learned how to get WordPress post featured image URL and post thumbnail. Do let me know in the comments below if you found the tutorial useful and if you’d like me to cover some other topics related to coding.

Also Read: Simple Methods To Optimize Images On WordPress Websites

Create Faster WordPress Websites!

Free eBook on WordPress Performance right in your inbox.


    Create Faster WordPress Websites!

    Free eBook on WordPress Performance right in your inbox.

      Owais works as a WordPress Community Manager at Cloudways and contributes occasionally at WPblog. He spends his time developing different websites on WordPress and finds special interest in WooCommerce.

      THERE'S MORE TO READ

      Newsletter

        WordPress Help Zone - Ultimate WordPress Pit-Stop

        Learning WordPress? Or are you expert enough to help others? Join our WP Facebook group!