Home » Functions » get_the_post_thumbnail

Function Name: get_the_post_thumbnail

In WordPress, the get_the_post_thumbnail function is used to retrieve the featured image (also known as the post thumbnail) of a specific post. This function allows developers to easily fetch the URL or HTML markup of the post thumbnail for further customization or display purposes.

The get_the_post_thumbnail function takes two parameters: the post ID and the desired thumbnail size. The post ID is the unique identifier for the post whose featured image you want to retrieve. The thumbnail size parameter is optional and specifies the dimensions of the thumbnail image to be returned. If no size is specified, the function will return the full-sized image.

This function is particularly useful in scenarios where you want to display the featured image in a different location or customize its appearance. For example, you might want to display the post thumbnail in a sidebar widget or use it as a background image for a specific section of your website.

Here’s an example usage code for the get_the_post_thumbnail function:

    $post_id = get_the_ID(); // Get the current post ID
    $thumbnail_url = get_the_post_thumbnail($post_id, 'medium'); // Retrieve the post thumbnail URL with the 'medium' size

    // Display the post thumbnail as an image with a specific class
    echo '<img src="' . $thumbnail_url . '" alt="Post Thumbnail" class="custom-thumbnail">';

In the example above, we first retrieve the current post ID using the get_the_ID function. Then, we use the get_the_post_thumbnail function to fetch the URL of the post thumbnail with the ‘medium’ size. Finally, we display the post thumbnail as an image using the retrieved URL and add a custom class for styling purposes.

Using the get_the_post_thumbnail function, you can effortlessly incorporate featured images into different parts of your WordPress website, enhancing its visual appeal and improving user engagement.

Learn More on

Register an account to save your snippets or go Pro to get more features.