Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Thanks!

share|improve this question

1 Answer 1

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 Mar 1 '12 at 20:16

Your Answer

 
discard

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.