In this quick tip, I’ll 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 we use to represent a particular blog post or page. Another name for featured images is post thumbnails.
The featured image section on the WordPress post edit page is not available by default. Support is added to a theme by adding the following line to the file functions.php file.
add_theme_support( 'post-thumbnails' );
If your theme supports featured images, you’ll see a Close-up image section in the edit screen of your posts as shown below.
How to get featured image in WordPress?
Let’s say a theme has added support for featured image functionality. In this case, you can use the get_the_post_thumbnail()
function to retrieve the foreground image of the specified post. This function will return the post thumbnail image tag. It accepts three optional parameters:
- the ID of the post you want to get the image for
- the size of the image
- attributes
If you provide no arguments, the function will return an image tag for the featured image of the current post by default.
<?php while ( have_posts() ) { the_post(); echo "<h1>".get_the_title()."</h1>"; echo get_the_post_thumbnail(); the_content(); } ?>
Another function you can use is the the_post_thumbnail()
function that eliminates the need to use echo
to produce the thumbnail of the post.
How to get 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 doesn’t have a featured image? In that case, this function will return the value 0.
In case you use this function to get the featured image ID of a particular post and it doesn’t exist, you will be returned false
as a return value. This ensures that a rigorous comparison reveals whether a particular post doesn’t have a featured image or if the post itself doesn’t exist.
<?php while ( have_posts() ) { // Some other code echo "<p>Thumbnail ID: ".get_post_thumbnail_id()."</p>"; } ?>
Try echoing the thumbnail ID within the loop and you’ll see it returns 0 for posts where no thumbnail has been provided. Additionally, a nonexistent Post ID will be returned false
as shown below.
<?php // Outputs: bool(false) var_dump(get_post_thumbnail_id(3468)); ?>
There are no posts with ID 3468 on my website, so come back false
.
How to check if a post contains a featured image?
You don’t have to rely on the return value of 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 takes an optional Post ID parameter and returns a Boolean value.
Will come back true
if the post has a thumbnail attached e false
otherwise.
<?php while ( have_posts() ) { the_post(); echo "<h1>".get_the_title()."</h1>"; if(has_post_thumbnail()) { echo get_the_post_thumbnail(); } else { // Show Placeholder Image } } ?>
You can use the value of this function to make layout decisions when showing a list of posts on the frontend.
Final thoughts
In this quick tip, I’ve shown you three different functions you can use to get a post featured image, featured image ID for a post, or check if a featured image exists.
There are many other post thumbnail features you should read about from the WordPress documentation.