Programmatically Pull Attachments from WordPress Posts
One of my favorite tricks in using WordPress as a CMS is to have it handle photo resizing and insertion into pages automatically. For example, swissmiss and I just launched Convert (a NYC based Green Roof Service) today. Within the projects section they display photos of the green roofing projects they’ve done and let you download a project sheet PDF.
It would be quite a burden for the client to have to know how to create all of the appropriate image sizes, upload the photos and insert the correct HTML tags. So here’s what we’ve done instead:
First, we set the WordPress image sizes to match the thumbnail and large sizes that we needed by going to Settings->Media in the admin tool.
Next, the client just has to upload all of their photos and a PDF for the project sheet. The images are automatically scaled by WordPress and attached to the page. They can also change the order the photos appear by dragging/dropping them in the admin tool.
Finally, adding this simple snippet of code to the functions.php in their theme, I can ask for all of the images associated with the post.
// get all of the images attached to the current post
function aldenta_get_images($size = 'thumbnail') {
global $post;
$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
$results = array();
if ($photos) {
foreach ($photos as $photo) {
// get the correct image html for the selected size
$results[] = wp_get_attachment_image($photo->ID, $size);
}
}
return $results;
}
So, if you want all of the thumbnail images (as an array of html tags) you can make the default call:
$photos = aldenta_get_images();
If you want all of the medium sized images you pass medium as the size:
$photos = aldenta_get_images('medium');
My function has a bit more going on inside of it but you get the idea of what you can do with this. Another way I like to use it is to pull the first image from the post. Sometimes, you want to display a thumbnail image that represents a post. Here’s how I like to do that:
// get the first image attached to the current post
function aldenta_get_post_image($size = 'thumbnail') {
global $post;
$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
if ($photos) {
$photo = array_shift($photos);
return wp_get_attachment_image($photo->ID, $size);
}
return false;
}
// the html tag for the first image or false if no image is found
$photo = aldenta_get_post_image();
What if you want to get the PDF attached to the post? No problem – it works the same but you change the mime type to application/pdf.
// get the first PDF attached to the current post
function aldenta_get_post_pdf() {
global $post;
$attachments = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'application/pdf', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
if ($attachments) {
$attachment = array_shift($attachments);
return wp_get_attachment_url($attachment->ID);
}
return false;
}
// the url to the first pdf or false if no pdf is found
$pdf = aldenta_get_post_pdf();
This technique has helped me tremendously and I hope it does the same for you.
42 Comments