AUTOMATIC WORDPRESS THUMBNAIL WITHOUT CUSTOM FIELD


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

This post will show you how to make your theme generate thumbnails automatically, based on your post’s first image. Uses timthumb to resize


Copy this code and paste it in your HTML
  1. <?php
  2. // retreives image from the post
  3. function getImage($num) {
  4. global $more;
  5. $more = 1;
  6. $content = get_the_content();
  7. $count = substr_count($content, '<img');
  8. $start = 0;
  9. for($i=1;$i<=$count;$i++) {
  10. $imgBeg = strpos($content, '<img', $start);
  11. $post = substr($content, $imgBeg);
  12. $imgEnd = strpos($post, '>');
  13. $postOutput = substr($post, 0, $imgEnd+1);
  14. $image[$i] = $postOutput;
  15. $start=$imgEnd+1;
  16.  
  17. $cleanF = strpos($image[$num],'src="')+5;
  18. $cleanB = strpos($image[$num],'"',$cleanF)-$cleanF;
  19. $imgThumb = substr($image[$num],$cleanF,$cleanB);
  20.  
  21. }
  22. if(stristr($image[$num],'<img')) { echo $imgThumb; }
  23. $more = 0;
  24. }
  25. //retreive image ends
  26. ?>
  27.  
  28. <!--thumnailcode-->
  29. <div class="thumbnail">
  30. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
  31. <img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php getImage('1'); ?>&w=150&h=150&zc=1">
  32. </a>
  33. </div>
  34.  
  35. <!--css-->
  36. .thumbnail a:link, .thumbnail a:visited {display:block; float:left; padding:5px; background:#e2e2e2; width:150px; height:150px; margin:5px 5px 0 0;}
  37. .thumbnail a:hover, .thumbnail a:active {background:#C4C4C4;}

URL: http://www.problogdesign.com/wordpress/automatic-wordpress-thumbnail-without-custom-field/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.