How to check for attachment and display a different image if not
-
Hi everyone,
I’m looking for a way to check if a post has an attachment; display the thumbnail if it does and another image if it doesn’t. I’ve tried the is_attachment to no avail.This is what I’ve done:
<?php if (is_attachment()) { ?><?php $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') ); foreach ( $attachments as $attachment_id => $attachment ) { echo wp_get_attachment_image( $attachment_id, 'thumbnail' );} ?><?php } else { ?>another image<?php } ?>
What the above code does is print another image, even if a post has an attachment.
All help is greatly appreciated and this will be my final question.
Thanks,
Gregory S.
- The topic ‘How to check for attachment and display a different image if not’ is closed to new replies.