Develop

WordPress: Display All Post Attachment Images In A Slider

The Issue

There are a lot of scenarios I can think of where you may need to do something like this, but let’s say for this particular example you would like to make your client’s life easy by allowing them to upload images to a post, and then having those images automatically output into an image slider on their published page…no plugins, no shortcodes, just magic.

Note

This method assumes you are familiar with editing theme files and including the necessary scripts and markup to create sliders. I’m not going through all of the steps involved with this, just the method for grabbing all of the photos attached to a particular post.

The Solution

We can pull all of the post attachments with the following function (add this to your functions.php file):

function revconcept_get_images($post_id) {
    global $post;

     $thumbnail_ID = get_post_thumbnail_id();

     $images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

     if ($images) :

         foreach ($images as $attachment_id => $image) :

         if ( $image->ID != $thumbnail_ID ) :

             $img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
             if ($img_alt == '') : $img_alt = $image->post_title; endif;

             $big_array = image_downsize( $image->ID, 'large' );
             $img_url = $big_array[0];

             echo '<li>';
             echo '<img src="';
             echo $img_url;
             echo '" alt="';
             echo $img_alt;
             echo '" />';
             echo '</li><!--end slide-->';

     endif; endforeach; endif; }

Note that this function DOES NOT include featured images attached to the post. If you want the featured image in the slider as well, see the alternate code in the example below.

This function pulls the images, with their alt attributes if they exist…if they don’t it will insert the post title as the alt attribute. It’s also setup to give you a choice in the size of the images that will be pulled, in this example it’s pulling the “large” image size, but you can change this to another image size, or even a custom image size that you’ve previously setup, just change “large” on this line: $big_array = image_downsize( $image->ID, 'large' );
The function outputs the images wrapped in <li> tags. For this example I was using FlexSlider by WooThemes (but you can easily modify this to work with another image slider). The last step in making this work, is to add the following to your template file:

<div class="flexslider">
    <ul class="slides">
        <?php revconcept_get_images("$post->ID"); ?>
    </ul>
</div><!--end flexslider-->

Again, note that some of this HTML markup is related to FlexSlider. The most important thing to include here is the <?php revconcept_get_images("$post->ID"); ?> which calls your function.

(UPDATED) Include Featured Images

If you want to include the featured images attached to your page/post, use this code instead:

function revconcept_get_images($post_id) {
    global $post;

     $thumbnail_ID = get_post_thumbnail_id();

     $images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

     if ($images) :

         foreach ($images as $attachment_id => $image) :

             $img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
             if ($img_alt == '') : $img_alt = $image->post_title; endif;

             $big_array = image_downsize( $image->ID, 'large' );
             $img_url = $big_array[0];

             echo '<li>';
             echo '<img src="';
             echo $img_url;
             echo '" alt="';
             echo $img_alt;
             echo '" />';
             echo '</li><!--end slide-->';

     endforeach; endif; }

Conclusion

That’s it! This is a really simple way to make creating sliders super easy with minimal steps for your clients (or yourself, I use this method in my portfolio on this site).

{ 138 Comments }

  1. Yo

    Hi Chris,
    could you explain me more precisely where to put this 2 piece of code “$img_cap = $image->post_excerpt;” and “” please?
    Thanks for your slideshow, that exactly what i needed!

  2. Ivar

    Hi, is it possible to make this function display each image caption? If it is, how is it done? Thanks!

    • Chris Perryman

      Sure thing! Just add this line to the function (after the $img_alt = line is fine):

      $img_cap = $image->post_excerpt;

      Then, add something like this below the img tag:

      < ?php echo $img_cap; ?>

      Let me know if that’s clear enough or I can send you more complete code.

    • Ivar

      Great! Thanks! It works like a charm!

  3. Marzia

    It is what I was looking for…. but I don’t know what is happening. It is attaching ALL media images to any post or page even if new and empty. A huge flexslider…..Gulp!! I put the fist code in functions.php, the second in page.php after the loop, just before the content. I have already flexslider working in home. What did I wrong? Many thanks!

    • Chris Perryman

      I’m not sure what you mean by “after the loop”, but it should be “inside” the loop. You may already have it there, if that’s the case I don’t know why it would be doing that.

    • Marzia

      yes, sorry, I meant inside… :-( I will try with a different theme and see if it does the same. Thank you anyway Chris.

    • TahinaTahina

      You should define or test the $post_id variable, unless it will pick all images attached to all posts.

    • Marzia

      Thank you Tahinatahina, you are right!
      $post_id = get_the_ID();
      and it’s working! Only I don’t understand why It has worked without it for everybody else!

  4. Jon Masterson

    This is great, but in your code, $img_title = undefined variable

    • Chris Perryman

      Nice catch. I for one, am not a big fan of the title attribute and rarely use it. I removed this from the code. Thanks!

  5. Hi Chris,

    I’m using your code on a wordpress site I’m developing – fantastic by the way, definitely what I was looking for – but It’s gone a bit weird for me…

    I’m using a plugin to show an attachments metabox on the post edit screen. This works great, the flexslider works perfectly.

    Until I try to use one of the images as a featured image.

    When I do that, the flexslider stops and only displays the featured image! However, I’ve got 2 other ‘galleries’ doing the same thing, but the flexslider works without an issue!

    The link to the dev site is http://thunderboltdev2.co.uk/homeconcepts/gallery/ – Fixtures/Fittings and Bedrooms are the galleries that are going astray…

    Thanks
    Mark

    • Chris Perryman

      That is very strange! I tried to recreate this on my site and another that I’m using this code on and the issue is not happening for me.

      What is the purpose of the attachments meta box for you? I would start troubleshooting by disabling that plugin and see if that resolves the issue…if not continue to disable plugins, it may be some sort of conflict.

      Worst case scenario, you could upload a duplicate photo to use as your featured image.

    • Hi Chris,

      After a bit of messing around, that’s eventually what I resorted to.

      I think it’s something to do with the metabox itself Im using. It tended to hang-up and freeze after I changed the attachments on the post. A bit of messing around and got it working…eventually!

      Thanks for coming back to me though! :)

      Mark

{ Respond }

Leave a response