• Hi!

    I’d like to find a way to echo the value of the url of the image defined as the_post_thumbnail.

    So far <?php the_post_thumbnail(); ?> returns the complete image, but I would like to be able to echo only the url.

    Is that possible?

    Thank you for your help!

Viewing 15 replies - 1 through 15 (of 18 total)
  • Untested, but something like this should work.

    get_permalink( get_post_thumbnail_id( $post->ID ) );

    If you didn’t mean the attachment URL, but the actual image path, ie. wp-content/ ..etc… then let me know and i’ll provide something else..

    Thread Starter Jeremy Herve

    (@hd-j)

    Thanks for the answer!

    Sorry if I was unclear. Indeed, I meant the image path. I want to echo that url in a meta tag, it is related to the Facebook open graph protocol.

    What I would like to achieve is something like that, but replace this header_logo by the_post_tumbnail

    <meta property="og:description" content="<?php echo strip_tags(get_the_excerpt($post->ID)); ?>" />
    	<meta property="og:type" content="article" />
    	<meta property="og:title" content="<?php single_post_title(''); ?>" />
    	<meta property="og:url" content="<?php the_permalink() ?>"/>
    	<meta property="og:image" content="<?php bloginfo('template_url') ?>/images/header_logo.png" />

    I hope this is more clear!

    Thank you for your help!

    Slight variation on before then..

    wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );

    …again untested but should work, echo or store as appropriate, function won’t print anything by default, so echo for quick testing..

    t31os_, I tried this as a workaround to an issue I was having — https://www.ads-software.com/support/topic/400895?replies=1 — and while it worked for pulling the image, it pulls the full image size instead of the optimized, thumbnail resized version that the_post_thumbnail() does.

    How would you access the WordPress-resized thumbnail variations with this?

    Just about to sit down and watch a movie but i’ll take a look for you when i return.

    ??

    Turned out to be pretty simple.

    wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) )

    Hope that helps.. ??

    Almost there! This one did pull a resized thumbnail, but the wrong one. It’s pulling the WP 150×150 standard.

    Fortunately I think my design accommodates for just resizing this one, but what I’m actually I’m looking to get the one designated by the user upon post, as supported by this function — https://codex.www.ads-software.com/Template_Tags/the_post_thumbnail . With this function you have the option of calling various sizes, but I can’t seem to find out if it’s possible to grab those same size variations using the get_attachment method?

    Maybe use this instead?
    https://codex.www.ads-software.com/Function_Reference/wp_get_attachment_image_src

    Or at least in conjunction with.

    get_post_thumbnail_id( $post->ID )

    Hmm, nope! Your input is definitely putting me on the right track, but it still looks like that call is still referencing the legacy WordPress thumbnail data. I’ll do some digging of my own and try to see what’s going on.

    I did find this snippet that sheds a bit of light, but I’m still having unpredictable results with the thumbnail its returning. Sometimes it pulls the properly-sized image I’m looking for, sometimes it pulls the default 150×150.

    <?php wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 60,60 ), false, '' ); ?>

    Sorry for derailing by the way, but t31os_ you’ve been a huge help! Thanks!

    Maybe..

    wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail' , false );

    ..else the array(60,60) sets the dimensions, and WordPress will just grab the closest sized image available(i think).

    Thread Starter Jeremy Herve

    (@hd-j)

    Thanks guys for your help. Unfortunately it still does not display anything for me, I have tried both functions you mentioned, but no value is returned.

    Any idea why this would happen?

    Thanks for your help!

    Did you echo? Each of those functions will need to be echoed to produce output, eg.

    echo wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) );

    t31os_, looks like you’re right. After some more troubleshooting, I’m pretty certain WordPress just pulls the closest-sized image based on the dimensions in that array. The reason you need to be careful though is that with (potential) multiple image sizes, it sometimes pulls the wrong image, so you have to do a lot of cross-referencing and troubleshooting with your WP Media settings, the post thumbnail designation, and your template code. Bottom line, there’s several places where things can break, giving some unexpected results.

    Anyway, thanks again for the answers. They’ve been super helpful!

    I’ve always found the media functions a little confusing in their usage myself, i can understand results being somewhat unexpected.

    The purpose and usage of these functions has changed over time though, so i can understand when things don’t quite gel together as i’d expect them to, considering they were written and added to WordPress at differing stages of development, but i’d imagine it will naturally improve over time.

    Happy to help all the same.. ??

    I’m trying to produce the same results as the OP. It seems we’ve decided that WP does not provide a function for calling just the image source of special sized post thumbnails. Anyone ever figure out a way around this?

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘echo the_post_thumbnail url’ is closed to new replies.