Created
August 1, 2012 18:41
-
-
Save ptasker/3229687 to your computer and use it in GitHub Desktop.
Get attachments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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