WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to return the basic url of a thumbnail? (16 posts)

  1. extatix
    Member
    Posted 4 years ago #

    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?

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  3. extatix
    Member
    Posted 4 years ago #

    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];
  4. extatix
    Member
    Posted 4 years ago #

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

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  6. extatix
    Member
    Posted 4 years ago #

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

  7. extatix
    Member
    Posted 4 years ago #

    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];
  8. Mark / t31os
    Moderator
    Posted 4 years ago #

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

  9. extatix
    Member
    Posted 4 years ago #

    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.

  10. Mark / t31os
    Moderator
    Posted 4 years ago #

    Thanks for sharing your code, i'm sure other users looking for something similar will find it very useful... :)

  11. aquabib
    Member
    Posted 4 years ago #

    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; ?>
  12. extatix
    Member
    Posted 4 years ago #

    Not in this theme no :)

  13. tobee
    Member
    Posted 3 years ago #

    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;"';
  14. GeertDD
    Member
    Posted 3 years ago #

    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()) ?>
  15. beakid
    Member
    Posted 3 years ago #

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

  16. beakid
    Member
    Posted 3 years ago #

    $thumbail_id in row 2 is obviously a misprint btw :P

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags