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.
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.
add_theme_support( 'post-thumbnails' );
If your theme supports featured images, you'll see a Featured image section on the edit screen of your posts as shown below.
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.
<?php while ( have_posts() ) { the_post(); echo "<h1>".get_the_title()."</h1>"; echo get_the_post_thumbnail(); the_content(); } ?>
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.
<?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 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.
<?php // Outputs: bool(false) var_dump(get_post_thumbnail_id(3468)); ?>
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.
<?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-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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.