WordPress.org

Ready to get started?Download WordPress

Forums

the_post_thumbnail URL (10 posts)

  1. elektrowolf
    Member
    Posted 4 years ago #

    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?

  2. Jonathan Dingman
    Member
    Posted 4 years ago #

    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

  3. Aeox
    Member
    Posted 4 years ago #

    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

  4. munikho
    Member
    Posted 4 years ago #

    Thanks, that worked for me actually.

  5. Humpake
    Member
    Posted 4 years ago #

    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.

  6. Humpake
    Member
    Posted 4 years ago #

    I of course mean $thumbnailsrc

  7. mmachine
    Member
    Posted 4 years ago #

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

  8. bhagwad
    Member
    Posted 3 years ago #

    Thanks Aeox - that worked!

  9. vodz0001
    Member
    Posted 3 years ago #

    Hi, i just came across same problem that array pulls out only 150x150. 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 150x150 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" />

  10. vodz0001
    Member
    Posted 3 years ago #

    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>

Topic Closed

This topic has been closed to new replies.

About this Topic