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. Mario

    Chris, is your code easily modified to link to larger sized images? A previous user commented back in 2015 and posted code to do this but it looks completely different from your code and it also doesn’t work. I’d like to stick with your code given how well it works but would like to add a minor tweak (if possible) to get the links. Any suggestions?

    • Chris Perryman

      You will want to modify this line where it’s calling the “large” attachment size:
      $big_array = image_downsize( $image->ID, ‘large’ );
      You can change ‘large’ to any size you want ;)

  2. Chris

    Excellent! Glad you got it working 🙌🏻

  3. Mario

    I figured it out. I found this page: https://woocommerce.com/flexslider/ Which instructs which files needed to be Enqueued. Thanks for the help and the ExcellentArticle!

  4. Mario

    Hi Chris. Thanks for the speedy response. I think that’s where my disconnect is. I am using the slider plugin but have not done anything else other than include your code in my functions.php and template file. For some reason though WordPress wasn’t accepting the Flexslider plugin when I tried to install it. Anyhow, I’ll have to look into figuring out which scripts from Flexislider need to enqueued and added to my page. If it worked for you then it should work for me. Is Enqueueing all I’d need to do ?

    • Chris Perryman

      I know you have this working but for others who may read the question: the flexslider.js (or min file) and the flexslider.css are all you should need enqueued on the page that is running the slider. Additionally that page should include the init script for the flexslider container and the function I reference above ;)

  5. Mario

    Hi Chris, I used your code and it successfully pulled a page’s attached images. I attempted to use Flexslider but I don’t thinks it’s compatible with the latest version of WordPress. Therefore I’m using Metaslider. I’ve changed the html markup to reflect Metaslider’s but all I get are static images and a nav dot for each image. Should your code work with Metaslider and if not, which slider other than Flex would you recommend?

    • Chris Perryman

      I’m not familiar with Metaslider, but yes…this should work with any slider. I know that Flexslider 2 works with the latest version of WP (http://flexslider.woothemes.com/). Can I ask, how are you including these sliders? When I use them, I do NOT use the plugin versions. I add the code directly to my theme or my own plugin and enqueue the necessary scripts and stylesheets. Have you checked your source code to make sure that the scripts that are needed to run that slider are being loaded on your page?

{ Respond }

Leave a response