Posted By

zartgesotten on 06/27/09


Tagged

hack image post wordpress attachment theme


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

maxvee8
jhonqwerty


wordpress - automatically create Greybox imageset from images attached to post


 / Published in: PHP
 

I pieced together this code when I had to do this for a client: - show the first image attached to a post (based on sort order in gallery tab) in medium size - put all the other images attached to the post into an imageset so that when you click on the preview image you will see a greybox-gallery to click through with the full size images.

  1. <!--Automatic Greybox-->
  2. <?php
  3. //Die zum Post gehoerenden Bilder ins Array $images schreiben
  4. //Put images from post in to array $images. Order by menu order defined in post gallery
  5. $images = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
  6.  
  7. //Pruefen, ob dem Beitrag Bilder angehaengt wurden.
  8. //check if there are any images attached to the post
  9. if (isset($images))
  10. {
  11. //Den Zaehler auf 0 setzen
  12. //set counter to 0
  13. $count=0;
  14. //Alle Bilder im Array $images nacheinander durchlaufen
  15. //go through all images in array $images
  16. foreach( $images as $image )
  17. {
  18. $imageID = $image->ID;
  19. //fuer jedes Bild im Array die URL von sowohl der Mittleren, als auch der grossen Groesse ermitteln.
  20. //get medium and large image url for image
  21. $medImageSrc = wp_get_attachment_image_src($imageID, $size='medium', $icon = false);
  22. $largeImageSrc = wp_get_attachment_image_src($imageID, $size='large', $icon = false);
  23.  
  24. //Wenn es sich um das erste Bild im Array handelt...
  25. //If it's the first image in the array
  26. if ($count==0)
  27. {
  28. //Den Greybox-Code mit einem Vorschaubild ausgeben
  29. //output Greybox Code with medium size preview
  30. echo"<a href='$largeImageSrc[0]' rel='shadowbox[Bilder]'><img src='$medImageSrc[0]' border='0'></a>";
  31. }
  32. //alle Bilder ,die nach dem erstern Bild kommen
  33. //this is for all the images that come after the first one
  34. else
  35. {
  36. //unsichtbare Greybox-Verknuepfung darstellen.
  37. //create invisible Greybox-Links without preview image
  38. echo"<a href='$largeImageSrc[0]' rel='shadowbox[Bilder]'></a>";
  39. }
  40. //Zaehler erhoehen.
  41. //increase counter
  42. $count++;
  43.  
  44. } //foreach
  45. }
  46.  
  47.  
  48.  
  49. ?>
  50.  
  51. <!--ENDE Automatic Greybox -->

Report this snippet  

You need to login to post a comment.