Recently while working on a custom project for a client, we had to get all post attachments from a custom post type and display them at one place. Because we were creating a grid display, we had each post’s featured image serving the purpose of a separator. This is why when getting all post attachments, we needed to exclude the featured image, so it doesn’t show up twice. In this article, we will show you how to get all post attachments in WordPress except for the featured image.
All you have to do is paste the following code inside a loop.
<?php if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) { $attachments = get_posts( array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $post->ID, 'exclude' => get_post_thumbnail_id() ) ); if ( $attachments ) { foreach ( $attachments as $attachment ) { $class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type ); $thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true ); echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>'; } } } ?>
The code above first checks if the post type is data-design and the post status is published. You may not need the first conditional depending on what you are trying to do. Then we simply run the get_posts query. Everything is pretty self explanatory there. The key that we must highlight is the exclude feature. That line is making sure that featured image does not show up. If you take that line away, then the featured image will show up. After specifying all the parameters, we simply specify what to do when each attachment is pulled. We are pulling attachment type for the class variable. Then the $thumbimg variable is simply using wp_get_attachment_link to pull the image at a specific thumbnail size, and it also hyperlinks the image to the single attachment pages. In the final step, we simply echo it.
We ran this code inside a loop with a separate call for featured image which links to the individual post. Final outcome looked something like this:
Each featured image served as an album identifying image which you can see. The grey spots were filled with the attachments for the post. We hope that this would help those who are looking to push WordPress beyond a blogging platform.
Hi
i try below code to get post attached image.
but always I update image it’s only same images shows.
please advice anyone.
‘attachment’
);
$attachments = get_posts($args);
if ($attachments)
{
foreach ($attachments as $attachment)
{
echo wp_get_attachment_image($attachment->ID, ‘full’);
}
}
endwhile;
endif;
?>
Hi,
I’ve been able to pull all images that I needed using your code and display them on a custom page. Can you please tell me how can I get these images to work like a gallery/slider?
Cheers,
Akshay
How to add restricon to it? Means if author already added gallery then I don’t want to show it on that post. Can we do this? Please Reply
I know this article is old, but thanks so much for this! I’ve been trying to figure out how to do this for a while now, for my theme that no longer has support.
but where should i write this code??
I really thanks to you posting this useful code spinet. Too helpful for making a custom plugin code.
Thanks so much
Weblizar
This is nice tutorial. I want to get image url instead of image attachment page as a image link
Great post! Thanks for sharing!!
I’ve inserted this code, but when I use it the page is blank. I’ve changed the custom post type name to match my own custom post type but still nothing shows.
Here’s the code:
post_type == ‘gallery’ && $post->post_status == ‘publish’ ) {
$attachments = get_posts( array(
‘post_type’ => ‘attachment’,
‘posts_per_page’ => -1,
‘post_parent’ => $post->ID,
‘exclude’ => get_post_thumbnail_id()
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$class = “post-attachment mime-” . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, ‘thumbnail-size’, true );
echo ” . $thumbimg . ”;
}
}
}
?>
Any help would be appreciated.
sorry I accidentally clipped the code:
post_type == ‘gallery’ && $post->post_status == ‘publish’ ) {
$attachments = get_posts( array(
‘post_type’ => ‘attachment’,
‘posts_per_page’ => -1,
‘post_parent’ => $post->ID,
‘exclude’ => get_post_thumbnail_id()
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$class = “post-attachment mime-” . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, ‘thumbnail-size’, true );
echo ” . $thumbimg . ”;
}
}
}
?>
I tried the code about and could not get the attachment for a cpt
I defined the cpt type by this query first:
how can i display full image in attachment not its thumbnail
Look in line 12 of the code where it says thumbnail-size. That’s really the image size. You can use one of the pre-existing sizes to show the image. The value “full” there would do the job.
problem:
1. insert a image into a post and press save
2. reload page with your code inside the loop, it works great
3. now remove the image from the post and save
4. reload and the image still there!!!!
This ins’t correct and I can’t find a solution for this on the internet
Thanks
Are you removing the image from the post? or are you deleting the image altogether? Because if you just remove the image code from the post content, then it will not go away. The image stays attached to the post it was uploaded too unless you DELETE it.
That’s what I figured it out.
Doesn’t make sense to me.
If you want to remove an image from a post and keep it for later use, the attachment will continue to appear using your code. I have to delete it!?
If instead of removing you just want to reuse the image in a second post, that second post will not show the image as attachment because there’s only one field for parent reference – parent_id – can’t point to 2 different IDs. I have to upload a second file equal to first one!?
This are very simples tasks that don’t work out of the box. Why is wordpress working like this? I believe that must be a very strong reason for that…
I’ve got the same problem. When you want to reuse image in another post i doesn’t work.
Doesn’t seem to work for me?
Got it working, I deleted this part…
‘$post->post_type == ‘data-design’ && ‘
Glad you were able to get it to work.
I think you should rename the post … “How to Get All Post Attachments in WordPress Except for Featured Image from a custom post type” as others like myself might see it as pertaining to all/any posts (which it doesn’t)… just might help (especially with search results)
You can easily do what you did above, and it works.
Dear Webmaster
You have not mentioned where to add codes in WordPress files is it functions.php, post.php or other things
Do please elaborate the same
@shankarsoma You have to paste it inside the loop. Most of the time, you will create a custom page and post it in the loop. In our case, we created a custom post type and ran it in that loop. So it is very subjective which file it will go in … It all depends on how you use it.