Support » Themes and Templates » the_post_thumbnail URL

  • The new the_post_thumbnail function in WP 2.9 is truly awesome! But it always outputs a whole link. Is there a possiblity to get just the image URL?

Viewing 9 replies - 1 through 9 (of 9 total)
  • What you could do is use this:

    function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
            echo get_the_post_thumbnail( NULL, $size, $attr );
    }

    So instead of the_post_thumbnail, use get_the_post_thumbnail() and it won’t echo it until you want it.

    I found this code in /wp-includes/post-thumbnail-template.php

    Hi,
    @jdingman. Didn’t work for me, since nowhere on the web is a snippet to found, I digged into the code and made this little code snippet:

    <?php
    //Get the Thumbnail URL
    $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 720,405 ), false, '' );
    echo $src[0];
    ?>

    Worked for me inside “the Loop”. You just need to change the array(width,height) part.

    ,Aeox

    Thanks, that worked for me actually.

    If you’re using PHP 5.* then it’s also possible to use simplexml to pull the src-attributes value or any other attribute value out of the get_post_thumbnail function. I worked this out:

    $domsxe = simplexml_load_string(get_the_post_thumbnail());
    $thumbnailsrc = $domsxe->attributes()->src;

    You can then echo or whatever you wish do do with $domsxe that now stores the src value as a string.

    I of course mean $thumbnailsrc

    Aeox — thanks for posting that code! Helped me start to solve a problem I’m having, but I’m getting some unpredictable results. Sometimes the proper post-associated thumbnail image is pulled, but other times the post gets the default 150×150 pulled instead.

    Can you elaborate a bit more on the context of the array() argument in the get_attachment() function? In other words, does it look for a thumbnail sized to the specified dimensions (as put into the array), or does that array tell WordPress the dimensions at which to render the pulled image?

    Thanks!

    Thanks Aeox – that worked!

    Hi, i just came across same problem that array pulls out only 150×150. I have a custom size called ‘slider’, how can i get img with this custom size outputted?
    Here is mu code inside the loop where it pulls the 150×150 img
    <?php
    $domsxe = simplexml_load_string(get_the_post_thumbnail());
    $thumbnailsrc = $domsxe->attributes()->src;
    ?>
    <li class=”sliderImage”>
    ” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”>
    <img src=”<?php echo $thumbnailsrc ?>” alt=”<?php the_title() ?>” width=”560″ height=”229″ />

    Sorry, the code is here

    <?php if (has_post_thumbnail()){ ?>
    <?php
    $domsxe = simplexml_load_string(get_the_post_thumbnail());
    $thumbnailsrc = $domsxe->attributes()->src;
    ?>
    
    <li class="sliderImage">
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    <img src="<?php echo $thumbnailsrc ?>" alt="<?php the_title() ?>" width="560" height="229" />
    
    </a>
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘the_post_thumbnail URL’ is closed to new replies.