I am using Wordpress to create a dynamic gallery using Bootstrap. I created a new custom field and then set it to 'images' and then assigned it to my post type. It then shows up in my post, I uploaded my images into the field and then attempt to put all the attachments into an array and then i will loop through them to display.

$args = array(
'post-type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()

$attachments = get_posts($args);

After doing this and doing a var dump with attachments, it says that nothing is in the array

  • 1
    'post-type' => 'attachment' should be 'post_type' => 'attachment' (underscore)
    – Shazzad
    Commented Oct 14, 2014 at 14:25
  • God that's so stupid, works perfectly now. Is there anyway to accept your comment. Thank you for the help Commented Oct 14, 2014 at 14:42

1 Answer 1


You can use this code with native WordPress Uploader. If you assing your images to custom field you must use get_post_meta like functions. The basic way, Upload your images from Add Media area to your post and use this code in your single.php or which theme file you use.

if ($migallery = get_children(
            'post_parent' => get_the_ID(),
            'post_type' => 'attachment',
            'numberposts' => -1,
            'post_mime_type' => 'image', 
            'exclude' => get_post_thumbnail_id()
            $aa_string .= "<div>"; //Before gallery div
            foreach ($migallery as $galerir)
                $aath            = wp_get_attachment_image_src($galerir->ID, 'thumbnail');
                $aabg            = wp_get_attachment_image_src($galerir->ID, 'full');
                $aa_string      .= "<a href='$aabg[0]'>";
                $aa_string      .= "<img src='$aath[0]'/>";
                $aa_string      .= "</a>";
            $aa_string .= "</div>"; //div ends
            $aa_string .= "<div style='clear:both;'></div>";
            return aa_string;

You can change code (esp. foreach area)for Bootstrap gallery.

  • Afraid that doesn't work Commented Oct 14, 2014 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.