Display All Attached Images to a WordPress Post

This will allow you to use Timthumb while showing all images within a WordPress post gallery. In other words, if I upload 4 photos to a post, they will automatically be shown wherever I insert this code in my template, within that post’s loop.

This is assuming that the timthumb script is in your theme’s directory: wp-content/themes/mytheme/timthumb.php

<?php
$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
	if ($attachments) {
	foreach ( $attachments as $attachment ) { ?>
      <img src="<?php bloginfo('template_url'); ?>/thumb.php?w=200&amp;h=200&amp;zc=1&amp;src=<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" alt="<?php the_title(); ?>" width="200" height="200" border="0" />
<?php	}
	} ?>

To have each thumbnail link to the full sized original image, have the anchor href point to

<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>

3 Comments
  1. Marty June 2, 20118:03 am

    Hey Chad! Great post! Hoping you might be able to help me solve a problem. What if I wanted to use this technique to display all of the attachments of a pages children?

    Setting post parent to ‘null’ returns all of the attachments for the site. I’ve also tried get_children to no avail… and the latest attempt was to set ‘post_parent’ => $pageChild->ID,

    Currently:
    $attachment_args = array(
    ‘post_type’ => ‘attachment’,
    ‘numberposts’ => -1,
    ‘post_status’ => null,
    ‘post_parent’ => $pageChild->ID,
    ‘orderby’ => ‘menu_order ID’
    );

    Thanks in advance!

    Marty

    • Chad Von Lind June 9, 201111:58 am

      Hi Marty, Unfortunately, I’m not able to help you on this one. You might find an answer to that question over at WP Stack Exchange. Be sure to vote up anyone that provides the working answer, and perhaps report back here with your findings.

  2. Marty June 10, 20113:29 am

    No worries – was curious if you had it in your bag of tricks :)

    Drop me a note via email if you are interested in WP related project work

Submit Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

To include your code, use pastebin, and paste the URL in the comment body. Failure to do so may prevent your comment from appearing. Thank you.