I want to know how to get just the image URL from the_post_thumbnail()
. The default output of the_post_thumbnail()
is:
<img width="800" height="533" src="http://example.com/wp-content/uploads/2011/02/book06.jpg" class="attachment-post-thumbnail wp-post-image" alt="book06" title="book06" />
Here I want grab the src
only. How do I filter the_post_thumbnail()
to get only http://example.com/wp-content/uploads/2011/02/book06.jpg
?
You might also try:
If you only have one size thumbnail:
Or…if you have multiple sizes:
Note that wp_get_attachment_image_src() returns an array: url, width, height, is_intermediate.
So if you just want only the image url:
Resources:
Since WordPress 4.4, there’s an efficient core function that can handle this in a cleaner way than the answers here.
You can use
the_post_thumbnail_url( $size )
which will print the URL of the post thumbnail.Alternatively if you want to return the URL instead of immediately output it, you can use
$url = get_the_post_thumbnail_url( $post_id, $size )
This does the trick:
Make sure you use the correct name for the thumbnail that you are calling.
Ok got it using
simplexml_load_string
Another method are welcome.
Please Use the below code
If It’s not enough to achieve your goal then try below code
For a quick & dirty solution, slap this in the
functions.php
file of your themeUsed within the loop, this will give you what you’re looking for
This will return something like
http://example.com/wp-content/uploads/2019/02/toy-story-two-was-ok.jpg
“Within the loop” means look for something like
while ( have_posts() ): the_post();
.You can also sub out
post-large
with any of these predefined image sizes:post-thumbnail
post-medium
post-full