How to Get the Featured Image in WordPress

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

What Is a Featured Image?

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

How to Get the Featured Image in WordPress

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.

How to Get the Featured Image ID in WordPress

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.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Monty Shokeen

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.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Monty Shokeen


Print Share Comment Cite Upload Translate Updates
APA

Monty Shokeen | Sciencx (2022-10-12T02:57:50+00:00) How to Get the Featured Image in WordPress. Retrieved from https://www.scien.cx/2022/10/12/how-to-get-the-featured-image-in-wordpress/

MLA
" » How to Get the Featured Image in WordPress." Monty Shokeen | Sciencx - Wednesday October 12, 2022, https://www.scien.cx/2022/10/12/how-to-get-the-featured-image-in-wordpress/
HARVARD
Monty Shokeen | Sciencx Wednesday October 12, 2022 » How to Get the Featured Image in WordPress., viewed ,<https://www.scien.cx/2022/10/12/how-to-get-the-featured-image-in-wordpress/>
VANCOUVER
Monty Shokeen | Sciencx - » How to Get the Featured Image in WordPress. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/12/how-to-get-the-featured-image-in-wordpress/
CHICAGO
" » How to Get the Featured Image in WordPress." Monty Shokeen | Sciencx - Accessed . https://www.scien.cx/2022/10/12/how-to-get-the-featured-image-in-wordpress/
IEEE
" » How to Get the Featured Image in WordPress." Monty Shokeen | Sciencx [Online]. Available: https://www.scien.cx/2022/10/12/how-to-get-the-featured-image-in-wordpress/. [Accessed: ]
rf:citation
» How to Get the Featured Image in WordPress | Monty Shokeen | Sciencx | https://www.scien.cx/2022/10/12/how-to-get-the-featured-image-in-wordpress/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.