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).
I’m trying to figure out how to get videos into this.. Do you have any tips?
I tried uploading the video as an attachment, and it counts it in as a slide. But it doesn’t show up when you go to that slide number. Ideally I would like to get vimeo and youtube videos into it as well.
This is my functions.php code:
function add_flexslider() { // display attachment images as a flexslider gallery on single posting
$attachments = get_attached_media( ”, $post->ID );
if ($attachments) { // if there are images attached to posting, start the flexslider markup
echo ”;
echo ”;
foreach ( $attachments as $attachment_id => $attachment ) { // create the list items for images with captions
echo ”;
echo wp_get_attachment_image($attachment_id, ‘full’);
echo ”;
echo get_post_field(‘post_excerpt’, $attachment->ID);
echo ”;
echo ”;
}
echo ”;
echo ”;
} // end see if images
} // end add flexslider
And for my template file, I use this:
Hi Nichlas…off the top of my head I’m not sure if this is possible or if it is what the issue would be. Could you send me a link to the site where you have this running?
Also, when you include code in your reply can you wrap it in the <pre> example I have below right above the “Post Comment” button? That way I will be able to see all the code you post.
Nice! Works great! Thank you! )))
And try add to flex init thumb navigation support. It,s work, but images box is empty!
(function($) {
$(window).load(function() {
$(‘.flexslider’).flexslider({
animation: ‘slide’,
controlNav: ‘thumbnails’,
controlsContainer: ‘.flex-container’
});
});
})(jQuery)
What is wrong?
If you look at the Flexslider documentation, you need to use a “data-thumb” attribute to assign the thumbnails (see here under the HTML tab: http://flex.madebymufffin.com/examples/thumbnail-controlnav.html).
So you should alter my code on line 20 like this:
Also, it would be a good idea to call an image that is not “large” (this is set on line 17). So you may want to add a couple lines below line 17 similar to this:
And then you would change my first sample on line 20 to this in order to call a thumbnail sized image vs. a large image:
Thank you for this function! I’m developing my first premium WP theme and needed to implement this feature for gallery post format. I’m using a custom slider but still works flawlessly.
Cheers!
This was super-useful. I used this for my portfolio. I am used to flex slider and built it out a little so I can have two, one in the front pulling sticky post images and one in a separate template pulling only what’s attached to a custom post called projects. However and this is because I am new to php completely. How do you use the get_post_meta to select only a few of the images? Using the code as is I haven’t been able to insert images in other sizes outside the slider as the function will be reading those as well, even though they’re meant to go outside of it.
Thanks a lot for the great demo!
Arghhhhh!
Only a few seconds later I reread the codex and found this: ‘numberposts’ => 0, let’s you select which pictures in your library to fetch.
Sorry for the total noob question. I must have read past this in stress. Have a good day!
Hey, no worries! Glad you got it working ;)
Hi Chris, seems to working fine, except, it slides only one image all the time I have it 3.
Copy paste code, so there is no missed type errors. Really don’t know what is going on?
Thanks!
Aleksandar, I’m sorry I somehow missed your comment – did you get this working? I would try adding:
in order to define the post ID. Let me know if that helps!