Skip to main content
there was missing a php end-tag
Source Link
Chip Bennett
  • 55.1k
  • 8
  • 90
  • 170

I found and modified a chunk of code found here: http://www.wprecipes.com/wordpress-tip-detect-if-a-post-has-at-least-one-image.

<?php  
while ( have_posts() ) : the_post();
    $content = $post->post_content;
    $searchimages = '~<img [^>]* />~';
    preg_match_all( $searchimages, $content, $pics );
    $iNumberOfPics = count($pics[0]);
    if ( $iNumberOfPics > 0 ) {
        //your loop content goes here
    } 
?>
<?php endwhile;  
?>

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.

I found and modified a chunk of code found here: http://www.wprecipes.com/wordpress-tip-detect-if-a-post-has-at-least-one-image.

<?php while ( have_posts() ) : the_post();
  $content = $post->post_content;
  $searchimages = '~<img [^>]* />~';
  preg_match_all( $searchimages, $content, $pics );
  $iNumberOfPics = count($pics[0]);
  if ( $iNumberOfPics > 0 ) {
    //your loop content goes here
  } 
?>
<?php endwhile; ?>

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.

I found and modified a chunk of code found here: http://www.wprecipes.com/wordpress-tip-detect-if-a-post-has-at-least-one-image.

<?php  
while ( have_posts() ) : the_post();
    $content = $post->post_content;
    $searchimages = '~<img [^>]* />~';
    preg_match_all( $searchimages, $content, $pics );
    $iNumberOfPics = count($pics[0]);
    if ( $iNumberOfPics > 0 ) {
        //your loop content goes here
    } 
endwhile;  
?>

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.

I found and modified a chunk of code found here: http://www.wprecipes.com/wordpress-tip-detect-if-a-post-has-at-least-one-image.

<?php while ( have_posts() ) : the_post(); ?>
  $content = $post->post_content;
  $searchimages = '~<img [^>]* />~';
  preg_match_all( $searchimages, $content, $pics );
  $iNumberOfPics = count($pics[0]);
  if ( $iNumberOfPics > 0 ) {
    //your loop content goes here
  } 
?>
<?php endwhile; ?>

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.

I found and modified a chunk of code found here: http://www.wprecipes.com/wordpress-tip-detect-if-a-post-has-at-least-one-image.

<?php while ( have_posts() ) : the_post(); ?>
  $content = $post->post_content;
  $searchimages = '~<img [^>]* />~';
  preg_match_all( $searchimages, $content, $pics );
  $iNumberOfPics = count($pics[0]);
  if ( $iNumberOfPics > 0 ) {
    //your loop content goes here
  }
<?php endwhile; ?>

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.

I found and modified a chunk of code found here: http://www.wprecipes.com/wordpress-tip-detect-if-a-post-has-at-least-one-image.

<?php while ( have_posts() ) : the_post();
  $content = $post->post_content;
  $searchimages = '~<img [^>]* />~';
  preg_match_all( $searchimages, $content, $pics );
  $iNumberOfPics = count($pics[0]);
  if ( $iNumberOfPics > 0 ) {
    //your loop content goes here
  } 
?>
<?php endwhile; ?>

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.

expanded code example
Source Link
Zach Lysobey
  • 839
  • 1
  • 15
  • 32

I found the followingand modified a chunk of code found 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.

<?php while ( have_posts() ) : the_post(); ?>
  $content = $post->post_content;
  $searchimages = '~<img [^>]* />~';
  preg_match_all( $searchimages, $content, $pics );
  $iNumberOfPics = count($pics[0]);
  if ( $iNumberOfPics > 0 ) {
     // Youryour postloop havecontent onegoes orhere
 more images.
}
<?php endwhile; ?>

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.

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.

I found and modified a chunk of code found here: http://www.wprecipes.com/wordpress-tip-detect-if-a-post-has-at-least-one-image.

<?php while ( have_posts() ) : the_post(); ?>
  $content = $post->post_content;
  $searchimages = '~<img [^>]* />~';
  preg_match_all( $searchimages, $content, $pics );
  $iNumberOfPics = count($pics[0]);
  if ( $iNumberOfPics > 0 ) {
    //your loop content goes here
  }
<?php endwhile; ?>

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.

Source Link
Zach Lysobey
  • 839
  • 1
  • 15
  • 32
Loading