Skip to main content
1 of 4
Zach Lysobey
  • 839
  • 1
  • 15
  • 32

I found the following chunk of code here: http://www.wprecipes.com/wordpress-tip-detect-if-a-post-has-at-least-one-image. If you wrap something resembling this around the stuff inside your loop, it should work.

$content = $post->post_content;
$searchimages = '~<img [^>]* />~';
preg_match_all( $searchimages, $content, $pics );
$iNumberOfPics = count($pics[0]);
if ( $iNumberOfPics > 0 ) {
     // Your post have one or more images.
}

I think this just looks for images entered in the rich text editor - if you want to check for featured images as well you'll likely have to add something. Check out has_post_thumbnail.

Zach Lysobey
  • 839
  • 1
  • 15
  • 32