Site logo

Archived topic

I want to get the featured Image URL of Blog Post

5 replies · Started by Sunny on November 1, 2020

Viewing posts 1–6 of 6

Hey,

I need to get the featured image URL of blog posts and then create a wp_head hook element with the below content...

<link rel="preload" as="image" href="<image URL here" />

Can you please guide me to add this to my website?

Hi,

You can get the featured image URL by using the get_the_post_thumbnail_url() core WordPress function.
Reference: https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

Try this snippet:

add_action( 'wp_head', function(){
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); 
echo '<link rel="preload" as="image" href="'.$featured_img_url.'"/>';
});

Thanks, the code works perfectly.

I'm closing this support thread.

Glad it works for you. No problem. :)

Hey @elvin how can I use this to preload my featured image which is loading as a background image? Do I just have to paste the snippet or do I have to do anything else?

Hi there,

background images behave differently so that method won't work.
If you want to raise a new topic where you can share a link to your site then we can take a look at what is possible

This archived topic is closed to new replies.