Jump to content

Image Script Mod - Check for image, Grab image


lucasw

Recommended Posts

Below is my current code, which I'm using in wordpress.  Here's what I'd like to do and can't figure out:

- Instead of displaying /images/default.jpg if there is no image found I would like nothing to be displayed.  Currently I get a empty box.

- Instead of running the below code first I would like <?php the_post_thumbnail(); ?> to be checked for an image, and if it is blank then the below code goes into action

 

Any help?

 

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];
  if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.