Advertisement
  1. Web Design
  2. WordPress

How to Get the Featured Image in WordPress

Scroll to top
Read Time: 3 min

In this quick tip, I will show you how to get the featured image for a post or page in WordPress.

A featured image in WordPress is the image that we use to represent a particular blog post or page. Featured images are also known as post thumbnails.

The featured image section on the WordPress post editing page is not available by default. Support for it is added to a theme by adding the following line to the functions.php file.

If your theme supports featured images, you'll see a Featured image section on the edit screen of your posts as shown below.

Featured Image Section on Edit PageFeatured Image Section on Edit PageFeatured Image Section on Edit Page

Let's say a theme has added support for the featured image functionality. In this case, you can use the get_the_post_thumbnail() function to retrieve the featured image of a specified post. This function will return the post thumbnail image tag. It accepts three optional parameters:

  • the post ID for which you want to get the image
  • the image size
  • attributes

If you don't supply any arguments, the function will return an image tag for the featured image of the current post by default.

Another function that you can use is the the_post_thumbnail() function, which removes the need for using echo to output the post thumbnail.

There is another useful function called get_post_thumbnail_id() which will return the post thumbnail ID for the current post. You can also pass a post ID as a parameter to the function to get the featured image of a particular post.

What if the current post does not have a featured image? In that case, this function will return the value 0.

If you use this function to get the featured image ID of a post that doesn't exist, you will get back false as the return value. This makes sure that a strict comparison will reveal if a particular post doesn't have a featured image or if the post itself doesn't exist.

Try echoing the thumbnail ID within the loop, and you'll see that it returns 0 for posts where no thumbnail has been provided. Also, passing a non-existent post ID will return false, as shown below.

There is no post with ID 3468 on my website, so it returns false.

How to Check If a Post Contains a Featured Image

You don't have to rely on the return value of the get_post_thumbnail_id() function to check if a post has set a featured image. You can do the same with another function called has_post_thumbnail(). This function accepts an optional post ID parameter and returns a boolean value.

It will return true if the post has an attached thumbnail and false otherwise.

You can use the value of this function to make layout-related decisions when showing a list of posts on the front end.

Final Thoughts

In this quick tip, I showed you three different functions that you can use to get the featured image of a post, the ID of the featured image for a post, or check if a featured image even exists.

There are many other functions related to post thumbnails that you can read about in the WordPress documentation.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.