WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking to determine how many images are attached to a post, specifically if there is only one. Basically if there are multiple images I want to display my slideshow script, if there is only one I want to display that image, and not load the slideshow.

I saw this on a similar post which determines if any images are attached:

$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image') );

if ( $attachments ) {

// do conditional stuff here 


Can I use something similar to determine if there is only one image?


share|improve this question
up vote 0 down vote accepted

That code should get the attachments as array which you should be able to use count() on to determine the total number of images. Example:

if ( $attachments ) {
    if ( 1 == count( $attachments ) ) {
      // handle single image
    } else {
      // handle multiple images
share|improve this answer
Perfect - this works fine, thanks for your help! – Owen Mar 1 '12 at 9:37
Great! Glad I could help. – Joseph Leedy Mar 1 '12 at 20:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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