Find first image in blog post and return it as a thumbnail


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

This will find the first image in post and return it as a thumbnail if it exists


Copy this code and paste it in your HTML
  1. function test_me() {
  2. $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
  3. if($files) :
  4. $keys = array_reverse(array_keys($files));
  5. $j=0;
  6. $num = $keys[$j];
  7. $image=wp_get_attachment_image($num, 'large', false);
  8. $imagepieces = explode('"', $image);
  9. $imagepath = $imagepieces[1];
  10. $thumb=wp_get_attachment_thumb_url($num);
  11. print "<img src='$thumb' class='thumbnail' />";
  12. endif;
  13. }
  14.  
  15. <?php test_me(); ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.