How To Get Featured Image In WordPress

In this blog post, we will discuss how to get the featured image in WordPress, which is an essential feature for displaying a website’s main image in a visually appealing manner. Featured images, also known as post thumbnails, are used to represent the content, mood, or theme of a post or a page. They are often displayed prominently at the top of a post or as a smaller thumbnail when using a grid layout for your blog.

1. Enable Featured Images

Before you can use featured images in your WordPress theme, you need to make sure that they are enabled. To do this, add the following code to your theme’s functions.php file:

add_theme_support( 'post-thumbnails' );

This code tells WordPress that your theme supports the use of featured images.

2. Set Featured Image Size

By default, WordPress will create a thumbnail size for your featured images. However, you can define custom sizes for your featured images by adding the following code to your functions.php file:

add_image_size( 'custom-thumbnail', 300, 200, true );

This code creates a new image size named “custom-thumbnail” with a width of 300px and a height of 200px. The “true” parameter indicates that the image should be cropped to fit the specified dimensions.

3. Display Featured Image in Your Theme

To display the featured image in your theme, you can use the the_post_thumbnail() function. This function should be placed inside the loop in your theme’s template files, such as single.php, archive.php, or index.php. Here is an example of how to display the featured image:

if ( has_post_thumbnail() ) {
    the_post_thumbnail( 'custom-thumbnail' );

This code checks if the post has a featured image using the has_post_thumbnail() function. If it does, it displays the featured image using the “custom-thumbnail” size that we defined earlier.

4. Retrieve Featured Image URL

If you want to get the URL of the featured image instead of displaying it directly, you can use the get_the_post_thumbnail_url() function. Here’s an example:

$featured_image_url = get_the_post_thumbnail_url( get_the_ID(), 'custom-thumbnail' );

This code retrieves the URL of the featured image for the current post, using the “custom-thumbnail” size. You can then use this URL as the src attribute for an tag, or as a background image in your CSS.


Featured images are an essential part of any modern WordPress website, as they help to create visually appealing and engaging content. By following the steps outlined in this blog post, you can easily enable and display featured images in your WordPress theme. Happy coding!