Posted By

creativelifeform on 05/17/10


Tagged

wordpress


Versions (?)

Catch that image


 / Published in: PHP
 

catches the first image of a post so you can display it however you like within the loop

  1. //grabs the first image from a post
  2. function catch_that_image()
  3. {
  4. global $post, $posts;
  5. $first_img = '';
  6. $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  7. $first_img = $matches [1] [0];
  8.  
  9. if(empty($first_img))
  10. { //Defines a default image
  11. $first_img = "/images/default.jpg";
  12. }
  13. return $first_img;
  14. }

Report this snippet  

You need to login to post a comment.