Posted By

distribuida on 09/14/11


Tagged

wordpress


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Kurt


wordpress thumbnails


 / Published in: PHP
 

URL: http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it

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

Report this snippet  

You need to login to post a comment.