Wednesday, October 12, 2022
HomeWeb DevelopmentFind out how to Get the Featured Picture in WordPress?

Find out how to Get the Featured Picture in WordPress?


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.

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.

In case your theme helps featured pictures, you’ll see a Featured picture part on the edit display of your posts as present beneath.

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

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.

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.

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.

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.

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.

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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments