Skip to content

Instantly share code, notes, and snippets.

@ptasker
Created August 1, 2012 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptasker/3229687 to your computer and use it in GitHub Desktop.
Save ptasker/3229687 to your computer and use it in GitHub Desktop.
Get attachments
<?php
/**
*
* Gets media attached to a post
*
*
* @global type $post
* @param type $post_id
* @return type array
* @todo revise method to be more flexible
*
*/
function get_attachments_by_post( $post_id, $file = false, $single = false, $image_size = "Full Size" ) {
global $post;
$out = array();
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post_id
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
if($file){
$out[] = get_the_post_thumbnail( $post_id, $image_size );
}else{
$out[] = get_attachment_link( $attachment->ID );
}
}
}
if ( $single && is_array( $out ) )
return isset($out[0])?$out[0]:'';
else
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment