X

Check if post has more than one image attached

A pretty simple if statement, wrote this up for my theme framework. In my case I used it to check if the post has more than one image display a slider, else display the single image.

Code Snippet:

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

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!