How to Check if a Post has More than One Image Attached

Here is a pretty simple statement that will check your post to see if there is more than one image attached.

<?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 } ?>

If you want to check the post to see if it has any image attachments at all, try this:

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

Leave a Reply

Your email address will not be published. Required fields are marked *