Skip to content

Instantly share code, notes, and snippets.

@matiskay
Created September 20, 2011 20:39
Show Gist options
  • Save matiskay/1230242 to your computer and use it in GitHub Desktop.
Save matiskay/1230242 to your computer and use it in GitHub Desktop.
Wordpress Snippets
<?php
// http://johnford.is/programmatically-pull-attachments-from-wordpress-posts/
// http://www.wprecipes.com/how-to-show-wordpress-post-attachments
// get all of the images attached to the current post
function _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;
}
// 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();$photos = _get_images('medium');
function aldenta_get_images($size = 'thumbnail') {
global $post;
return 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 = aldenta_get_images()) {
foreach ($images as $image) {
echo $image->post_title; // title
echo $image->post_content; // description
echo $image->post_excerpt; // caption
}
}
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo apply_filters('the_title', $attachment->post_title);
the_attachment_link($attachment->ID, false);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment