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).
Hi Chris,
I had a bit of a fiddle with it, and have added a few bits: a small array to display the image as thumbnails, with a large array which acts as a href for a link I’ve added. Thought you might appreciate it, if anyone asks…
I’ve also edited what gets displayed for the alt and the link title =)
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);
if ($img_alt == ”) : $img_alt = $post->post_title . ‘ – ‘ . $image->post_name;
endif;
$thumb_array = image_downsize($image->ID, ‘medium’);
$big_array = image_downsize($image->ID, ”);
$img_thumb = $thumb_array[0];
$img_url = $big_array[0];
echo ‘‘;
endif;
endforeach;
endif;
}
hmmmm it appears it didn’t like my echo statement in the comment, it should be:
echo ‘‘;
Thank you for sharing! If you look under the comment box below, you will see a wrapper that will protect your code from formatting issues ;)
Hey Chris! Thanks for the awesome code!
I’m having a small problem with it. I’m usign easy lightbox on my theme so all the images will open in a lighbox. I’ve modified the code on functions.php so that the images being displayed are linked to their full sizes. With the exception of the first image on the slider, all of the others works just fine and are show in the lightbox when clicked. Do you have any idea what causes the first image to behave diferently?
Thank you.
Is the first image in your slider also the “featured image” for that page/post?
Initially it was, so we tried removing the featured images, but the problem continues :/
Ok, so I would try to upload the image to the post again…it may still be in the DB as being connected to the featured image. Also, for a future solution to this problem, read the updated note on this post (I just added it above).
Thanks! we’ll try that :)
I’m having a bit of a problem with this.
The scripts load, the slider loads and I can see the images in the code, but they’re not visible. Any ideas what could cause this? The formatting looks correct, but I can’t see anything. :O
Could there be a problem in the script init?
Thanks in advance. :)
Do you have a URL that I could look at?
Hey :)
I got the images showing. It was script initialization problem. Now I’m having problems with showing the featured image.
If I take this line out if ( $image->ID != $thumbnail_ID ) : the script works. If I keep it in nothing works. If I remove the != $thumbnail_ID part it falls apart again…
Scratch that last message. I don’t know what I was writing.
What I meant to say is that it won’t show the featured image for some reason… I have if ( $image->ID ) : in there. Without the != $thumbnail_ID part in.
I’m using the same featured image for many posts. Can that be a reason?
You’re correct. Try removing line 12, and the first endif on line 28.
Hello Chris,
I’m still looking to solve why the featured image doesn’t show up in the flex slider. I set the url as my website there.
Best regards
Hello,
I tried removing line 12 and the first endif; but it didn’t have any effect on the site. It still works, but doesn’t get the featured image in. :/
Any more ideas? :) Everything is appriciated! Would be nice to not have to manually put the same image twice to the same article.
Can you please create a gist (or similar) and paste the code you are using so I can look at it?
Sure,
Here’s the links:
functions.php (for the function): https://gist.github.com/anonymous/28d59e790e42f3af5149
single.php (where I call the pictures): https://gist.github.com/anonymous/b42770bbf04b90725df7
Thanks in advance!
I tested your code and it’s working fine. I would suggest possibly removing the current featured image, saving the page, then coming back in and adding it and saving again – see if that resolves it.
There should be something wrong with the , looks like it is not functioning well!
Hi CHRIS,
Thanks for sharing but I am having issue on running your solution on single.php. What I have a CPT called “projects” and a projects-single.php as:
$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 ”;
echo ”;
echo ‘‘;
endif; endforeach; endif;
but this is returning all of images in the gallery regardless of the parent ID, I mean I just need to get images attached the that particular post but I am getting all images attached to all posts! Can you please let me know why this is happening and how I can fix it?
Once again Thanks
Try adding:
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.
Thanks for reply Chris,
but I am not getting anything now just a white empty page! , I also used the print_r( $images ); statement before if($image) and I am getting an empty Array ( ) in the page.By the way, I think I wrapped the comment cod by your but this time I did for sure.
$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 ”;
echo ”;
echo ‘‘;
endif; endforeach; endif;
I can’t see the code you’re pasting – if you want to put it into a gist I can take a look there. If you are getting a white screen, undo your last set of changes, there is a php error somewhere.
Ok ,
Here is the code which I have:
https://raw.githubusercontent.com/Behseini/Easing/master/Code
Thanks,
Where are you adding this code? It’s working for me – and so is the
. This code should be added to your functions.php file, or your functionality plugin if you’ve created one.
Then, on the theme page where you want this to appear, you need to add: