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.

 

 

<?php
$attachments = get_children(
array(
'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 } ?>

Be Sociable, Share!

Leave a comment