Extract first image from a post (Wordpress)


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



Copy this code and paste it in your HTML
  1. function get_image_src($posti = null) {
  2. if (!$posti)
  3. global $post;
  4. else
  5. $post->post_content = $posti;
  6.  
  7. $the_content = $post->post_content;
  8. $pattern = '!<img.*?src="(.*?)"!';
  9. preg_match_all($pattern, $the_content, $matches);
  10. $image_src = $matches['1'][0];
  11. return $image_src;
  12. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.