Posted By

blogdojo on 04/22/09


Tagged

image wordpress attachment


Versions (?)

Who likes this?

9 people have marked this snippet as a favorite

atma
ivanrsl
lilumi
tmbrenneke
maxvee8
nickreese
margot620
davebowker
ToddSmithSalter


get wordpress image attachment and use tim thumb to crop


 / Published in: PHP
 

  1. <?
  2.  
  3. // usage <?php postimage(180,135);?>
  4. // or whatever dimensions you want
  5. // requirements: have tim thumb in your theme directory
  6.  
  7. function postimage($width,$height) {
  8. $scriptpath = get_bloginfo('template_directory');
  9. $attachments = get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
  10. if (!is_array($attachments)) {
  11. $image = $scriptpath."/images/default.gif";
  12. echo '<img src="'.$scriptpath.'/scripts/timthumb.php?src='.$image.'&w='.$width.'&h='.$height.'&zc=1" alt="" />';
  13. }
  14. else {
  15. $img = array_shift($attachments);
  16. $imagelink = wp_get_attachment_image_src($img->ID,'full');
  17. $image = $imagelink[0];
  18. echo '<img src="'.$scriptpath.'/scripts/timthumb.php?src='.$image.'&w='.$width.'&h='.$height.'&zc=1" alt="" />';
  19. }
  20. }
  21.  
  22. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ivanrsl on June 18, 2009

How do I use the_title () in the alt and title tags?

Posted By: ivanrsl on June 18, 2009

How do I use the_title () in the alt and title tags?

Posted By: michaeloeser on December 27, 2009

Since there is a change in WP 2.9 it doesn´t work with this version anymore. Is there an update available?

You need to login to post a comment.