0

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!

0

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
    }
}
  • Great! Glad I could help. – Joseph Leedy Mar 1 '12 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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