It’s not completely clear if you want to show one image or all image for each post. If you want to show just one, I’d use the post thumbnail:
- Check for a post thumbnail. If there’s a post thumbnail – output it (for simplicity I’m assuming you’d output it in a new template part; you could simply include it inline using
the_post_thumbnail()
); - Otherwise include the post as currently (or a placeholder image if you’d prefer).
if(get_post_thumbnail_id()):
get_template_part('item-image-only');
else:
// fallback
endif;
If you need to output all images, you can use get_children
– e.g. :
$attached_images = get_children( array(
'post_parent' => $post->ID ,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order') );
(and you can then check if an item has images by checking count($attached_images)
)