WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] echo the_post_thumbnail url (19 posts)

  1. Jeremy Herve
    Member
    Posted 4 years ago #

    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!

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    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..

  3. Jeremy Herve
    Member
    Posted 4 years ago #

    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!

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    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..

  5. mmachine
    Member
    Posted 4 years ago #

    t31os_, I tried this as a workaround to an issue I was having -- http://wordpress.org/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?

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

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

    :)

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    Turned out to be pretty simple.

    wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) )

    Hope that helps.. :)

  8. mmachine
    Member
    Posted 4 years ago #

    Almost there! This one did pull a resized thumbnail, but the wrong one. It's pulling the WP 150x150 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 -- http://codex.wordpress.org/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?

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    Maybe use this instead?
    http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

    Or at least in conjunction with.

    get_post_thumbnail_id( $post->ID )
  10. mmachine
    Member
    Posted 4 years ago #

    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 150x150.

    <?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!

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

    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).

  12. Jeremy Herve
    Member
    Posted 4 years ago #

    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!

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    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 ) );
  14. mmachine
    Member
    Posted 4 years ago #

    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!

  15. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.. ;)

  16. Demonhood
    Member
    Posted 4 years ago #

    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?

  17. tenaciousJk
    Member
    Posted 4 years ago #

    Found in another thread:

    <?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 300,300 ), false, '' ); echo $src[0]; ?>

    300,300 represents the dimension of your thumbnail. In my case the smallest being 350 x whatever. It will automatically choose the closest size to your thumbnail.

  18. deezlee
    Member
    Posted 4 years ago #

    hey, i'm using this code to show today's event in the sidebar:

    <?php $temp_query = $wp_query; ?>
    
    <?php 
    
    $today = getdate();
    query_posts('category_name=Events&posts_per_page=1&year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"] );
    ?>
    
    <?php while (have_posts()) : the_post(); ?>
    
    <div style="float:left;" class="thickbox"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail( array(92,69) ); ?></a></div>
    
    <b><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></b>
    <?php the_excerpt(); ?>
    
    <div class="deezclear"></div>
     <?php endwhile; ?>
    <?php $wp_query = $temp_query; ?>

    The thumbnail currently links to the post url, i want to make it link to the full size image instead.
    I gather that this is the code to use:
    wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
    but i'm not sure how to apply it...

    i know that i need to "echo" it, but i can't get the syntax right.
    any chance someone could show me how to put those codes together?

    thanks!

  19. dollypower
    Member
    Posted 3 years ago #

    @tenaciousJk Thanks, this worked perfectly for me. x

Topic Closed

This topic has been closed to new replies.

About this Topic