Unable to get all the Posts Attachments

I want to retrieve all the image attachments from a posts and render it in a gallery. Some how the number attachments which is being returned is only one even tough the posts contains multiple image attachments. Strangely some of the attachments which are beings returned are not even present in the posts. Below is the function which I am using to get the attchments

function get_attchments($num=1){
$output = ”;
if (has_post_thumbnail() && $num == 1){
$output = wp_get_attachment_url(get_post_thumbnail_id(get_the_ID()));
}else {
$attachments = get_posts(array(
‘post_type’ => ‘attachment’,
‘posts_per_page’ => $num,
‘exclude’ => get_post_thumbnail_id(),
‘post_parent’ => get_the_ID(),

if ($attachments && $num == 1){
foreach ($attachments as $attachment){
$output = wp_get_attachment_url($attachment->ID);
}else if($attachments && $num > 1){
$output = $attachments;
return $output;

I’ve been with this issues since long. Thank you

Read more here:: Unable to get all the Posts Attachments

Leave a Reply

Your email address will not be published. Required fields are marked *