Wordpress Image Gallery Short Code Using Post Attachments


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

UPDATE: might be easier or faster to use this now (also courtesy Pippin): http://pippinsplugins.com/get-all-attached-media/

**courtesy Pippin Williamson (http://pippinsplugins.com/image-gallery-short-code-using-post-attachments/)

use shortcode [photo_gallery] to display the gallery


Copy this code and paste it in your HTML
  1. function pippin_gallery_shortcode( $atts, $content = null ) {
  2.  
  3. extract( shortcode_atts( array(
  4. 'size' => ''
  5. ), $atts ) );
  6.  
  7. $image_size = 'medium';
  8. if($size =! '') { $image_size = $size; }
  9.  
  10. $images = get_children(array(
  11. 'post_parent' => get_the_ID(),
  12. 'post_type' => 'attachment',
  13. 'numberposts' => -1,
  14. 'orderly' => 'menu_order',
  15. //'exclude' => get_post_thumbnail_id(), -- uncomment to exclude post thumbnail
  16. 'post_mime_type' => 'image',
  17. )
  18. );
  19.  
  20. if($images) {
  21. $gallery = '<ul class="gallery clearfix">';
  22. foreach( $images as $image ) {
  23. $gallery .= '<li>';
  24. $gallery .= '<a href="' . wp_get_attachment_url($image->ID) . '" rel="shadowbox">';
  25. $gallery .= wp_get_attachment_image($image->ID, $image_size);
  26. $gallery .= '</a>';
  27. $gallery .= '</li>';
  28. }
  29. $gallery .= '</ul>';
  30.  
  31. return $gallery;
  32. }
  33.  
  34. }
  35. add_shortcode('photo_gallery', 'pippin_gallery_shortcode');

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.