The get_the_post_thumbnail() WordPress function will allow you to get the featured image and display it using your theme or plugin. In this article, we will teach you how to use the get_the_post_thumbnail() function to display featured images anywhere you want to.

If you would like more information on creating WordPress plugins, see our tutorial series on creating your first WordPress plugin.

Note: To use the get_the_post_thumbnail() function, you will need to enable support for it within your theme.

To enable support for the get_the_post_thumbnail() function, include the following in your theme's functions.php file:

add_theme_support( 'post-thumbnails' );

Basic usage

<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>

In the above code, we are simply displaying the featured image of a post based on the parameters we have defined. Below, you will see the various parameters that can be defined within the get_the_post_thumbnail() function.

Parameters

$post_id: This is an optional parameter in which you can define the post ID of the post that you will be obtaining the featured image from. If this is not defined, it will display the featured image from the current post that you are viewing.

$size: This is an optional parameter that can be used to determine the size that the featured image is displayed. You may define either a string or an array with the dimensions.

Pre-defined sizes:

  • thumbnail

  • medium

  • large

  • full

Custom sizes:

To define a custom size, simply define it within an array, for example:

get_the_post_thumbnail($post_id, array(100,100), $attr );

As you can see from the above code, the image size is 100x100.

$attr: This is an optional parameter in which you may use an array to define additional attributes such as the class, alt text, title, etc. This would look something like the following:

$attr = array(
'src' => $src,
'class' => "attachment-$size",
'alt' => trim(strip_tags( $attachment->post_excerpt )),
'title' => trim(strip_tags( $attachment->post_title )),
);

In the above example, we are storing the array containing the image location, class, alt text, and title within a variable which is later called within the get_the_post_thumbnail() function.

References

For more information on the get_the_post_thumbnail() function, take a look at the get_the_post_thumbnail() WordPress codex page.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
n/a Points
2014-07-08 3:33 am
Give the parameters when we use a loop to get the post id.
Staff
17,354 Points
2014-07-08 1:02 pm
Hello Kallol,

For more explicit information concerning the POST ID, please reference the WordPress codex on this function. Here is the specific link to the get post reference: get post meta.

Kindest regards,
Arnel C.

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

2 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!