Skip to content

Gallery of all attached images in WordPress

For a site I’ve been working one I wanted to automatically include a gallery of all the images attached to a post, and I wanted it to display exactly like a standard WordPress gallery would look.  The HTML is modeled after the gallery code from Twenty Thirteen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
$args = array(
     'numberposts' => -1,
     'orderby' => 'menu_order',
     'order'=> 'ASC',
     'post_mime_type' => 'image',
     'post_parent' => $post->ID,
     'post_status' => null,
     'post_type' => 'attachment'
);
 
$images = get_children( $args );
 
if($images){ ?>
     <h2>Photos</h2>
 
     <div id='gallery-1' class='gallery galleryid-10 gallery-columns-4 gallery-size-thumbnail'>
          <?php foreach($images as $image){
               $image2 = wp_get_attachment_image_src( $image->ID, 'thumbnail'); ?>
               <dl class='gallery-item'><dt class='gallery-icon landscape'>
               <a href='<?php echo $image->guid; ?>'><img width="150" height="150" src="<?php echo $image2[0]; ?>" /></a>
               </dt></dl>
          <?php } ?>
          <br style='clear: both;' />
     </div>
<?php } ?>
Published inCSS/HTMLPHPScripting

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *