Getting post thumbnail URL (12 posts)

  1. nemci7v
    Posted 5 years ago #

    I need just the image source of post thumbnails for a js slider I'm trying to integrate.

    For example the loop will look like this

    <img src="thumbnail url" longdesc="URL_1" alt="Text_1" />
    <img src="thumbnail url" longdesc="URL_2" alt="Text_2" />
    <img src="thumbnail url" longdesc="URL_3" alt="Text_3" />

    so in the img src I need to somehow echo the post thumbnails url. Can someone please share how to do this .. if possible?

  2. I haven't tested this, but you might be able to use this (or something like it...):

    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    <img src="<?php echo $url; ?>" longdesc="URL_2" alt="Text_2" />


  3. nemci7v
    Posted 5 years ago #

    Thanks trepmal!

    I did try that code, it works but it gets the full size image url. For example,


    the url for the thumbnail would be


    I can't find any documentation on how to get the exact URL for the generated thumbnail.. :(

  4. Maybe this then?

    <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail_size' );
    $url = $thumb['0']; ?>


  5. nemci7v
    Posted 5 years ago #

    Still generates the full size URL


    this is weird. Using the_post_thumbnail(), the URL path is the thumbnail's but I'm not sure how that's generated.

  6. oops, I forgot to specificially mention that thumbnail_size should be the name of the thumbnail size you're using (I think the default is 'post-thumbnail'?) - did you try swapping that out?

    Here is where the the_post_thumbnail() function is defined, you might be able to follow the trail: http://phpxref.com/xref/wordpress/wp-includes/post-thumbnail-template.php.source.html#l40

  7. nemci7v
    Posted 5 years ago #

    Trepmal you're awesome! post-thumbnail works :) I'll add the code to my functions, it's very useful :)

  8. glad you got things working out

  9. pealo86
    Posted 5 years ago #

    Worked like a treat, thanks ;)

  10. lybsolutions
    Posted 5 years ago #

    Worked for me, thanks. Below is how I used it in it's entirety - just in case anyone wants a quick C'nP.

    <?php query_posts('category_name=Portfolio&order=ASC&orderby=date&posts_per_page=36'); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
    $url = $thumb['0'];
    <li><a style="background-image:url(<?=$url?>);" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"></a></li>
    <?php endwhile; ?>
    <?php endif; ?>
  11. Chozen
    Posted 5 years ago #

    I've tried everything I've written here and nothing is working for me.

    Can one of you help me please?

  12. @Chozen Can you post what code you have so far? If it's lengthy, please use something like http://pastebin.com/.

Topic Closed

This topic has been closed to new replies.

About this Topic