Featured images are a great way to make your content more engaging, since adding a featured image to WordPress Posts may significantly enhance the readability of your content. Featured images can be used for blog posts, pages, and custom post types.
If you are a WordPress developer and building themes and plugins from scratch or even modifying existing themes and plugins to include featured images to pages, posts, or custom posts, then how to get featured image URL in WordPress is something that you’ll really want to know.
So in this guide, I’m going to show how you can get a featured image URL in WordPress in two ways.
Note: First, make sure that your WordPress theme has enabled support for featured images.
Enable support for Featured Image with Php code
Most of the themes already have featured image support enabled, but if you are developing a theme from scratch or your current theme does not have this functionality enabled, then insert this code in function.php.
<?php //Enable theme support for featured images add_theme_support('post-thumbnails'); ?>
Note: function.php file is located in Appearance > Theme Editor > and then on the top-right side of your screen is the function.php file
Still, if you don’t know how to edit function.php file, then I have written separate guide on it to help you easily edit it.
Hence, if your theme doesn’t include post thumbnail functionality, you won’t be able to add a featured image while editing a post.
As you can see, there is no option available to add a featured image to the post
but after adding the above code in the function.php file, you will be able to add a featured image to the post
Now that you’ve enabled support for the featured image, the next thing on your mind is how to get the featured image URL. So I’ll be demonstrating two ways to get the featured image URL.
You May like :
1) Get Featured Image URL with Inspect Element
To get the featured image(thumbnail) URL of a post or a page, locate any of the posts or pages on your website and right-click on the featured image( thumbnail ), and click on ‘inspect element.’
So, on the right side of your screen in developer tools, you will see the URL of the featured image
However, you can also get the featured image URL by right-clicking the featured image ( thumbnail )on any post and then click on the copy image address
So the featured image URL is now copied to your clipboard
2) Get Featured Image URL with Php code
WordPress has its built-in function get_the_post_thumbnail() to display featured images for posts, pages, and attachments based on various parameters such as height, id, width, etc.
<?php //Displays the featured image in a <img> tag (use this in a loop) echo get_the_post_thumbnail(); ?>
This is the easiest approach to display a post’s featured image in the famous WordPress loop.
If you want the function to display the thumbnail( featured image ) of the current post then you add this parameter in get_the_post_thumbnail().
<?php echo get_the_post_thumbnail( $post_id ); ?>
you can also adjust the size of thumbnails( featured images ) by giving a second argument of size in the above code so,
<?php echo get_the_post_thumbnail( $post_id, "medium"); ?>
Note: you can give different sizes as a second argument like medium, large or full( original size ).
Now, you can use get_the_post_thumbnail_url() function to return the featured image URL.
<?php //Display the featured post URL (you can replace 'medium' with different image size) echo get_the_post_thumbnail_url( $post_id, "medium"); ?>
So with this code, you can get the featured image URL of a post. You can use this code with WordPress loop or anywhere else that requires post thumbnail functionality. If you want to dig deep into how the get_the_post_thumbnail() function works, you can read it here.
FAQ(How To Get Featured Image URL in WordPress)
How do I find the URL of a featured image in WordPress?
get_the_post_thumbnail_url() is a built-in WordPress function that returns the URL of the featured image for a post or a page. However, if you want to save this address, you’ll need to create a variable for it.
$featured_img_url = get_the_post_thumbnail_url( $post_id)
Hence the URL is now stored in that variable and you can use it anywhere you want
How can I get the featured image URL in a WordPress post or page?
You can get the featured image URL in WordPress by using a PHP function “
get_the_post_thumbnail_url()” here is the code show URL:
echo get_the_post_thumbnail( $post_id );
As you can see, getting the featured image URL in WordPress is pretty simple and straightforward. I ve explained different methods to get featured image URL in WordPress. These methods should work regardless of the theme because it’s how you’re getting images from the WordPress database.
I hope this article helps you in how to get the URL of the featured image. If you have questions or clarifications, feel free to ask in the comments section below.