How to Get WordPress Post Featured Image URL

Are you a WordPress developer or an advanced WordPress user? Whoever you are, sometimes you may need the direct URL of a featured image of your posts and display it into your posts. Let’s find some easy way to get the WordPress post featured image URL.

How to Display a Featured Image

The easiest way to display the featured images in your custom theme or somewhere else is by adding the following code:

<div style="max-width: 100%"><?php the_post_thumbnail(); ?></div>

In the official documentation of the WordPress function the_post_thumbnail() right here is written as a description “Display the post thumbnail”. This function has as the first parameter size, so you can write the function with the size you need.

Default WordPress and WooCommerce image sizes

<?php

//Default WordPress
the_post_thumbnail( 'thumbnail' );    
the_post_thumbnail( 'medium' );       
the_post_thumbnail( 'medium_large' ); 
the_post_thumbnail( 'large' );         
the_post_thumbnail( 'full' );        
 
//With WooCommerce
the_post_thumbnail( 'shop_thumbnail' ); 
the_post_thumbnail( 'shop_catalog' );   
the_post_thumbnail( 'shop_single' );  

Get WordPress Post Featured Image URL

I suppose that you are using a newer version than WordPress 4.4 (if you not, you are crazy) you can use the built-in WordPress function the_post_thumbnail_url() function to return the URL of the featured post image.

<?php
//Replace 'medium' with another image size
the_post_thumbnail_url( 'medium' );

Proper usage of the function get_the_post_thumbnail_url() inside the loop

if ( have_posts() ) {
    while ( have_posts() ) {

        the_post();

        $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); 

        echo '<a href="'.esc_url($featured_img_url).'" rel="lightbox">'; 
            the_post_thumbnail('thumbnail');
        echo '</a>';

    }
}

Feel free to write a comment with any questions.

Recent Articles

How to find WooCommerce products in the database

Hey, where in the database (or phpMyAdmin) are my WooCommerce products stored? Sometimes you may have some reason to find directly any...

Why is WordPress Asking For Your FTP Credentials to Install Plugins?

You want to install any plugin, but *blink* - WordPress need your FTP credentials. What the hell? The problem is simple -...

Fix PHP Warning: POST Content-Length of 8978294 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

This error is getting probably when you trying to upload an import on WordPress on your XAMPP local dev environment, right? Well,...

How to Hide Shipping Methods in WooCommerce?

This is a small detail, but WooCommerce doesn't have built-in any function for hiding shipping methods based on different situations. Imagine when...

How to Get WordPress Post Featured Image URL

Are you a WordPress developer or an advanced WordPress user? Whoever you are, sometimes you may need the direct URL of a...

Get in touch

Be our first-class reader, build a community and follow us on social media!

Leave A Reply

Please enter your comment!
Please enter your name here