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).
This is a great tutorial. I was wondering if you knew how to call in the custom URL of one of post attachments? I’ve tried using the “get_post_meta( $post->ID, ‘_wp_attachment_url’, true );” but it seems to break the slider….
Also, in thinking about this, how would a url of an image be saved if the images are just called via the attachment call?
Hi Aaron,
The URLs are already setup in the code in my sample, just not being called into action, these are the lines that set them up and gets them:
If you want to include a link around the image, for example, you can change what I have at line 20 like this:
NOTE: remove the rel=”nofollow” from the “< a >” tag tag in the code above. WordPress inserts that automatically into links in comments, but it shouldn’t be there on your site.
Hi Chris,
This is an incredibly useful code, thanks.
I was wondering, how is it possible to customize the size of the images that I am grabbing, I see you set it to large, but I want to do it using pixel values, and set maximum width or height.
How can I do so? Thanks!
Hi Zahi,
Of course! This is fairly simple, although you may need to re-upload (or use a plugin to reset) your images and their thumbnails. WordPress lets you add custom image sizes, so that, every time you upload a photo it will have a copy saved in the size you define. By default, there are already three image sizes setup in WordPress, these are: thumbnail, medium and large…and of course the original size you uploaded ;)
If you want to add another, custom size you can do that by adding the following lines of code to your
functions.php
file:< ?php add_image_size( $name, $width, $height, $crop ); ?>
…where
$name
is your custom name (ex: my-size),$width
and$length
are your desired width and length in pixels and$crop
is either set totrue
orfalse
, depending on whether or not you want WP to chop your photo to get it to fit your dimensions or not after first attempting to resize it.Please refer to this article in the Codex for more information and examples: http://codex.wordpress.org/Function_Reference/add_image_size
Also, note that you would change “large” in my example to whatever you name your new size ;)
Hi there, this is extremely useful! top work.
I was just wondering if you knew of a way to use this functionality but to display the images shown using the gallery shortcode within wordpress posts rather than attached post items? Thanks again for sharing!
Hi Phil, I’m not sure I understand what you mean? Are you trying to limit the output to specific attachments?
Yes I was trying to output what had been put in the gallery shortcode of a post rather than the attachments of the post. I have literally just managed to do this! I used:
get_post_gallery_images
and created a function that loops through the gallery images. Here is my full function…function pw_show_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() )
return $content;
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve the first gallery in the post
$gallery = get_post_gallery_images( $post );
// Loop through each image in each gallery
foreach( $gallery as $image ) {
//$full_image_url = get_post_gallery_images();
$image_list .= '';
}
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
remove_shortcode('gallery');
add_shortcode('gallery', 'custom_size_gallery');
function custom_size_gallery($attr) {
$attr['size'] = 'full';
return gallery_shortcode($attr);
}
Then in my post template, echo the function into a flexslider. Ooh and its also important to use a function that defaults the gallery size to full as otherwise it loops through the thumbnail size. Hopefully this may be useful to others!
Sorry $image_list should be $image_list .= ‘‘;
Glad you were able to get this working and thanks for sharing your solution!
To Phil and Christ :
i tried to echo on my template to get the album shown on flexslider but didn’t work. I used :
What’s wrong ? Suggestions ?
Thanks in advance
Lorenzo, could you try posting your code again? Nothing showed up. Wrap the code in:
This is great!!! Thank you. It all works perfectly for me but I loose my Featured Image… Is there a way to include into the slider as well? PS I have included the Featured Image into the actual post as an image too.
Sure thing. If you want to include the featured image in the slider just remove this bit of code:
!= $thumbnail_ID
In the example above, that was on line 12.
Awesome, Thanks Chris :)
Thanks alot for this, been banging my head for two days trying to figure it out, even resulted in using metaboxes.
Welcome ;)