Get Image URL of WordPress Post Thumbnail Feature

In my recent work, I have to get image URL of post thumbnail. This is a bit difficult because WordPress doesn’t provide a function to get the URL, but only funtion to get a full HTML code. I’ll share the solution here.

Get Image URL of WordPress Post Thumbnail Feature

Because the post thumbnail actually is an attached image to a post, you could use wp_get_attachment_image_src() to get the URL. Place the following code in the loop:

<?php 	

	$imageArray = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail_name' );
	$imageURL = $imageArray[0]; // here's the image url

 ?>

You could replace thumbnail_name with ‘thumbnail‘, ‘medium‘, ‘large‘ or any thumbnail name you set via add_image_size function.

With this code, You’ll have more freedom to use image from WordPress thumbnail post feature.

8 Comments to “Get Image URL of WordPress Post Thumbnail Feature”

Add Comments (+)

  1. Charlotte says:

    Thanks for this post – it’s very helpful.

  2. Ben says:

    I was looking for a way to have all the posts image in an array. I found a way to have all images with html tags, but not the pure URL of the image. And that is what I need.

  3. Ramees says:

    VERY VERY THANKS FOR THIS POST.

  4. ice says:

    Thanks., How can i use this code .
    can you show example . ^^!

  5. LucasMorato says:

    Thanks so much! :DD

  6. johanso says:

    Thanks for this post..

  7. somebody says:

    little, but helpful thins, thank you

Trackbacks/Pingbacks

  1. 40 Best WordPress Tips and Tutorials of December 2010

Leave a Reply

 

Amazingly Beautiful WordPress Themes