WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to return the basic url of a thumbnail?

[Resolved] How to return the basic url of a thumbnail?

  • Hi,

    I’m trying to add a function where the ‘full’ thumbnail from the thumbnail function in WP 2.9+ is returned, but I only want the location of the thumbnail. Ergo, just http://mysiteisco.ol/thumbnail.jpg

    I’ve tried

    if ( function_exists('has_post_thumbnail') && has_post_thumbnail($id) ) {
     $thumbnail = get_the_post_thumbnail($post->ID, 'full' );

    But that returns something which doesn’t work. Can anybody tell me what to use instead of get_the_post_thumbnail?

Viewing 15 replies - 1 through 15 (of 15 total)
  • Take a look at.
    http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

    It’s how wp_get_attachment_image gets the path, which in turn is what’s used by the post thumbnail functions.

    I’m not sure if I understand this right, I changed it into but now there’s no output at all.

    if ( function_exists('has_post_thumbnail') && has_post_thumbnail($id) ) {
     $thumbnail = wp_get_attachment_image_src(get_the_post_thumbnail($post->ID, 'full' )); 
    
     		if (!$image_src[0]) return false;
    		else return $image_src[0];

    I changed it into

    if ( function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID) ) {
     $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
    
    		if (!$thumbnail[0]) return false;
    		else return $thumbnail[0];

    But it still doesn’t do what I want. Hm…

    You only need to provide an ID to that function, see the return values shown.
    http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src#Return_Values

    The return value of get_the_post_thumbnail($post->ID, 'full' ) will not be a numeric value (an ID), therefore it’s invalid to place it inside the function call to wp_get_attachment_image_src.

    Print your variables out, it’ll make more sense when you know what’s in them.

    I’ve tried a couple of things and I can’t get this one right.

    And I did it… Man, thanks t31os_, you lead me into the right direction.

    The end result. This gave me the thumbnail I wanted. But I wanted to put this into TimThumb. So I exchanged the last part after else return to that ‘function’. Great.

    if ( function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID) ) {
     $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), full );
    
    		if (!$thumbnail[0]) return false;
    	else return $thumbnail[0];

    I could of fixed it for you, but then you’d not feel the same sense of accomplishment as you do right now.. 😉

    Happy to hear you got it working… 🙂 I knew you would .. 😉

    I was really getting annoyed because it didn’t work the way I wanted it. It did not give me the picture to throw in timthumb, it showed me the picture.

    I was standing under the shower and then I finally realised what went wrong, I never said the $thumbnail had to go to Timthumb. After that it was a walk in the park.

    For completeness, this is the complete end result.

    Thanks for sharing your code, i’m sure other users looking for something similar will find it very useful… 🙂

    Is this the simple way?

    <?php
    while ( $posts->have_posts() ) : $posts->the_post();
    $src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), full );
    if ( has_post_thumbnail() ) { ?>
    <img src="<?php bloginfo('template_url'); ?>/inc/timthumb.php?src=<?php echo $src[0]; ?>&h=348&w=600&zc=1" />
    <?php } else { ?>
    <img src="<?php bloginfo('template_url'); ?>/inc/timthumb.php?src=<?php bloginfo('template_url'); ?>/images/default.png&h=348&w=600&zc=1" />
    <?php }
    endwhile; ?>

    Not in this theme no 🙂

    I wrote a little helper function:

    function get_the_post_thumbnail_data($intID = 0) {
        if($intID == 0) {
            return $intID;
        }
        $objDom = new SimpleXMLElement(get_the_post_thumbnail($intID));
        $arrDom = (array)$objDom;
        return (array)$arrDom['@attributes'];
    }
    $arrImage = get_the_post_thumbnail_data($wp_query->post->ID);
                $strStyle = ' style="width: 760px; height: ' . $arrImage['height'] . 'px; background: transparent url(' . $arrImage['src'] . ') scroll no-repeat 0 0;"';

    I wrote a quick little helper function too:

    function get_the_post_thumbnail_src($img)
    {
      return (preg_match('~\bsrc="([^"]++)"~', $img, $matches)) ? $matches[1] : '';
    }

    Example usage:

    <?php $image_url = get_the_post_thumbnail_src(get_the_post_thumbnail()) ?>

    There is already a built-in function to do what you want. No need for regexp and other dirty solutions.

    $thumbnail_id = get_post_thumbnail_id($post->ID);
    $thumbnail_object = get_post($thumbail_id);

    After that you can just call things like $thumbnail_object->guid (this is the src to the image) and $thumbnail_object->post_title. Try running an print_r($thumbnail_object) and you’ll see what other goodies are stored there 🙂

    $thumbail_id in row 2 is obviously a misprint btw 😛

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘[Resolved] How to return the basic url of a thumbnail?’ is closed to new replies.