Catch that image


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.