WordPress.org

Ready to get started?Download WordPress

Forums

Ouput URLs for thumb, medium and large images (10 posts)

  1. juxprose2
    Member
    Posted 5 years ago #

    Is it possible to grab just the URL for all images attached to a post?

    I need to extract the URL for thumb, medium and large.

    The URLS will fit into something like this within the loop:

    <ul>
    <li>
    <a href="PATH TO MEDIUM IMAGE" original="PATH TO LARGE IMAGE">
    <img src="PATH TO THUMBNAIL IMAGE"></a>
    </li>
    </ul>

    This will be true for all images attached to the post.

    This post is pretty close, but I can only get it to work for one image size (medium): http://wordpress.org/support/topic/140609?replies=43

    Any ideas?

    Thanks

  2. juxprose2
    Member
    Posted 5 years ago #

    OK, so this code will happily output the 3 different image sizes for all images attached to a post:

    <?php if ( $images = get_children(array(
    'post_parent' => get_the_ID(),
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    ))) : ?>
    
    <ul>
    <?php foreach( $images as $image ) :  ?>
    <li><?php echo wp_get_attachment_link($image->ID, 'thumbnail'); ?></li>
    <li><?php echo wp_get_attachment_link($image->ID, 'medium'); ?></li>
    <li><?php echo wp_get_attachment_link($image->ID, 'full'); ?></li>
    <?php endforeach; ?>
    </ul>
    
    <?php else: // No images ?>
    <!-- This post has no attached images -->
    <?php endif; ?>

    Now, does anyone know how the above could be modified to just output the URL, not the <img src"">? I would like to make up the <img src=""> myself.

    Any help would be great, thanks.

  3. juxprose2
    Member
    Posted 5 years ago #

    Perhaps a better way of describing this would be that I want to pull all image attachments from a post and output all 3 sizes as "http://PATH TO IMAGE".

    This code will output the http://PATH TO IMAGE.jpg for each image attached the post.....but it just outputs the large image ref 3 times - it can't separate between thumb,medium and large.

    <?php if ( $images = get_children(array(
    'post_parent' => get_the_ID(),
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    ))) : ?>
    <ul>
    
    <?php foreach( $images as $image ) :  ?>
    <li><?php echo wp_get_attachment_url($image->ID, 'thumbnail'); ?></li>
    <li><?php echo wp_get_attachment_url($image->ID, 'medium'); ?></li>
    <li><?php echo wp_get_attachment_url($image->ID, 'full'); ?></li>
    <?php endforeach; ?>
    </ul>
    
    <?php else: // No images ?>
    <!-- This post has no attached images -->
    <?php endif; ?>

    I'm getting close, but don't know much PHP, so any suggestions would be helpful, thanks.

  4. juxprose2
    Member
    Posted 5 years ago #

    Does anybody know if this is possible?

  5. juxprose2
    Member
    Posted 5 years ago #

    Anyone?

  6. Lumpysimon
    Member
    Posted 5 years ago #

    You can use the function wp_get_attachment_image_src, which returns an array. The URL of the image is in $whatever_your_array_is_called[0]. there's a full description in the codex:

    http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

    hope that helps!

  7. takien
    Member
    Posted 5 years ago #

    looking for this too

  8. taeke
    Member
    Posted 4 years ago #

    @juxprose2; did you manage to come up with a solution? I'm trying to achieve something similar..

  9. youtag
    Member
    Posted 4 years ago #

    thanks juxprose. that was what i needed.

  10. left_coaster
    Member
    Posted 4 years ago #

    Yeah I was looking for the same thing and the wp_get_attachment_image_src() function did the trick. I'm using WP 2.9 beta right now and wanted to display the post image as the background to a <div> with my post title in it.

    <?php if ( has_post_image() ) {
      $image_id = get_post_image_id();
      $post_image_data = wp_get_attachment_image_src( $image_id, $size='medium' ); ?>
    <div id="listing-image" style="background:transparent url(<?php echo $post_image_data[0]; ?>) no-repeat scroll 0 0;height:<?php echo $post_image_data[2]; ?>px;">
       <h2><?php the_title(); ?></h2>
    </div><?php } ?>

    Works great! Thanks for the pointer :)

Topic Closed

This topic has been closed to new replies.

About this Topic