[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

  • This topic has 3 replies, 2 voices, and was last updated 4 months ago by Elvin.
Viewing 4 posts - 1 through 4 (of 4 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?"

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