On this fast tip, I’ll present you the best way to get the featured picture for a put up or web page in WordPress.
What’s a Featured Picture?
A featured picture in WordPress is the picture that we use to symbolize a selected weblog put up or web page. One other title for featured pictures is put up thumbnails.
The featured picture part on the WordPress put up enhancing web page isn’t accessible by default. The assist is added to a theme by including the next line to the capabilities.php file.
add_theme_support( 'post-thumbnails' );
In case your theme helps featured pictures, you’ll see a Featured picture part on the edit display of your posts as present beneath.
Find out how to Get the Featured Picture in WordPress?
Let’s imagine a theme has added assist for featured picture performance. On this case, you need to use the get_the_post_thumbnail()
operate to retrieve the featured picture of specified put up. This operate will return the put up thumbnail picture tag. It accepts three non-obligatory parameters:
- the put up ID for which you need to get the picture
- the picture dimension
- attributes
Should you do not provide any arguments, the operate will return a picture tag for the featured picture of the present put up by default.
<?php whereas ( have_posts() ) { the_post(); echo "<h1>".get_the_title()."</h1>"; echo get_the_post_thumbnail(); the_content(); } ?>
One other operate that you need to use is the the_post_thumbnail()
operate which removes the necessity of utilizing echo
to output the put up thumbnail.
Find out how to Get the Featured Picture ID in WordPress?
There’s one other helpful operate referred to as get_post_thumbnail_id()
which can return the put up thumbnail ID for the present put up. It’s also possible to move a put up ID as parameter to the operate to get the featured picture of a selected put up.
What if the present put up doesn’t have a featured picture? In that case, this operate will return the worth 0.
In case you employ this operate to get the featured picture ID of a selected put up and it does not exist, you’re going to get again false
because the return worth. This makes positive {that a} strict comparability will reveal if a selected put up does not have a featured picture or if the put up itself does not exist.
<?php whereas ( have_posts() ) { // Another code echo "<p>Thumbnail ID: ".get_post_thumbnail_id()."</p>"; } ?>
Attempt echoing the thumbnail ID throughout the loop and you will notice that it returns 0 for posts the place no thumbnail has been offered. Additionally, passing a non-existent put up ID will return false
as proven beneath.
<?php // Outputs: bool(false) var_dump(get_post_thumbnail_id(3468)); ?>
There is no such thing as a put up with ID 3468 on my web site so it returns false
.
Find out how to Test if a Publish Comprises a Featured Picture?
You do not have to depend on the return worth of the get_post_thumbnail_id()
operate so as to examine if a put up has set a featured picture. You are able to do the identical with one other operate referred to as has_post_thumbnail()
. This operate accepts an non-obligatory put up ID parameter and returns a boolean worth.
It is going to return true
if the put up has an hooked up thumbnail and false
in any other case.
<?php whereas ( have_posts() ) { the_post(); echo "<h1>".get_the_title()."</h1>"; if(has_post_thumbnail()) { echo get_the_post_thumbnail(); } else { // Present Placeholder Picture } } ?>
You should utilize the worth of this operate to make structure associated choices when exhibiting an inventory of posts on the frontend.
Ultimate Ideas
On this fast tip, I confirmed you three totally different capabilities that you need to use to get the featured picture of a put up, the ID of the featured picture for a put up, or examine if a featured picture even exists.
There are lots of different put up thumbnail associated operate that you need to examine from the WordPress documentation.