Home / attachment / Check if post has more than one image attachment
  • How to Check if post has more than one image attachment


    Adding this snippet to your wordpress theme will let you determine if a post has more than one image attachment or modify the count to check for a specific number of image attachments.

    ( click code to copy )

    wordpress snippet : PHP

    $attachments = get_children(
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'post_parent' => $post->ID
    if(count($attachments) > 1) { ?>
        <!-- Do something like show a slider -->
    <?php } else { ?>
        <!-- Display a single image -->
    <?php } ?>

    ( WordPress codex functions, hooks, in this snippet. )

  • you saved my life with this snippet!!! thanks!!!!

  • guillermo

    oh my god! thank you man! this is awesome.

  • Gizmoscoop


    Your are the man. I’ve spent the whole night and tearing my hair out just trying to do this. I finally searched for this phrase “php to display wordpress gallery if there are 2 images or more” and got your website. You are the greatest!

  • Juan

    Awesome! Thanks a lot. This really worked for me.

  • Stagger Lee

    You can renew this snippet. Will be very useful to theme developers for Blog icons if post has gallery.
    Now when WordPress has select dropdown custom image sizes even for galleries. People can chose only one image in gallery and put it at the top of the Post. Needed for Jetpack carousel for instance.
    This snippet will make it easy to dismiss all Posts with “one image” gallery (not really gallery).