wordpress - automatically create Greybox imageset from images attached to post


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

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.