33

I want to know how to get image url on the_post_thumbnail()

Default the_post_thumbnail()

<img width="800" height="533" src="http://domain.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() only to get http://domain.com/wp-content/uploads/2011/02/book06.jpg

Let me know

41

You might also try:

If you only have one size thumbnail:

$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) );

Or...if you have multiple sizes:

$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "size" );

Note that wp_get_attachment_image_src() returns an array: url, width, height, is_intermediate.

So if you just want only the image url:

echo $thumbnail[0];

Resources:

|improve this answer|||||
  • A little hint: if you are using wp_get_attachment_image_src() function with size and want to get exact thumbnail size: use thumbnail name given in definition (function add_image_size()). If you use array with dimensions WP will use first image size that have proper width or height. So you may get wrong image. Example: instead of 156x98 you might have got 120x98 if you have 2 images defined: 156x98 & 120x98 (height is the same). I fell for it once ;) – Marek Tuchalski Oct 16 '11 at 15:15
2

This does the trick:

<?php wp_get_attachment_image_src('subgall-thumb'); ?>

Make sure you use the correct name for the thumbnail that you are calling.

|improve this answer|||||
  • 1
    I don't know if this has changed since 2012, but in 2017 the first parameter of wp_get_attachment_image_src must be the attachement id number, not the size. – squarecandy May 11 '17 at 22:07
2

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( $size )

|improve this answer|||||
0

Ok got it using simplexml_load_string

$dom = simplexml_load_string(get_the_post_thumbnail());
$src = $dom->attributes()->src;
    echo $src;

Another method are welcome.

|improve this answer|||||
0

Please Use the below code

<?php get_the_post_thumbnail_url(); ?>

If It's not enough to achieve your goal then try below code

<?php $postimages = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );

// Check for images if ( $postimages ) {

    // Get featured image   $postimage = $postimages[0];

} else {} while (have_posts() && $i < 8) : the_post(); echo esc_url( $postimage ); ?>
|improve this answer|||||
0

For a quick & dirty solution, slap this in the functions.php file of your theme

FUNCTION GET_STRING_BETWEEN($STRING, $START, $END){
    $STRING = " ".$STRING;
    $INI = STRPOS($STRING, $START);
    IF ($INI == 0) RETURN "";
    $INI += STRLEN($START);
    $LEN = STRPOS($STRING, $END, $INI) - $INI;
    RETURN SUBSTR($STRING, $INI, $LEN);
}

Used within the loop, this will give you what you're looking for

This will return something like http://foo.com/wp-content/uploads/2019/02/toy-story-two-was-ok.jpg

$THE_FEATURED_IMAGE = GET_STRING_BETWEEN(get_the_post_thumbnail(NULL,'post-large'), 'src="', '" class="');

* "Within the loop" = 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

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.