[Resolved] I want to get the featured Image URL of Blog Post

Home Forums Support [Resolved] I want to get the featured Image URL of Blog Post

Home Forums Support I want to get the featured Image URL of Blog Post

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1513995
    Sunny Kumar

    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?

    #1514023
    Elvin
    Staff
    Customer Support

    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.'"/>';
    });

    A wise man once said:
    "Have you cleared your cache?"

    #1514041
    Sunny Kumar

    Thanks, the code works perfectly.

    I’m closing this support thread.

    #1514043
    Elvin
    Staff
    Customer Support

    Glad it works for you. No problem. 🙂

    A wise man once said:
    "Have you cleared your cache?"

    #2149297
    Ella

    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?

    #2149338
    David
    Staff
    Customer Support

    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

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.