InMotion Hosting Support Center

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.


$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.


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

Was this article helpful?

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

Support Center Login

Our Login page has moved, Click the button below to be taken to the login page.

n/a Points
2017-07-04 10:57 am

It helped alot. Thanks.

41,747 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.
n/a Points
2014-07-08 3:33 am
Give the parameters when we use a loop to get the post id.

Post a Comment

Email Address:
Phone Number:

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

3 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


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:
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!