wpexplorer

WPEx Gallery Shortcode - WordPress

Sep 2nd, 2012
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. /*--------------------------------------*/
  2. /* WPEX Gallery
  3. /*--------------------------------------*/
  4. function wpex_gallery_shortcode( $atts, $content = null ) {
  5.  
  6. extract( shortcode_atts( array(
  7. 'columns' => '4',
  8. 'image_height' => '',
  9. 'exclude' => '',
  10. 'lightbox' => ''
  11. ), $atts ) );
  12.  
  13. $output = '';
  14. global $post;
  15.  
  16. //get attachement count
  17. $get_attachments = get_children( array( 'post_parent' => $post->ID ) );
  18.  
  19. //get ID of featured image
  20. $featured_image_id = get_post_thumbnail_id( $post->ID );
  21.  
  22. //generate random number for each gallery
  23. $rand_gallery = rand();
  24.  
  25. //attachement loop
  26. $args = array(
  27. 'orderby' => 'menu_order',
  28. 'post_type' => 'attachment',
  29. 'post_parent' => get_the_ID(),
  30. 'post_mime_type' => 'image',
  31. 'post_status' => null,
  32. 'posts_per_page' => -1,
  33. 'exclude' => $exclude
  34. );
  35. $attachments = get_posts($args);
  36.  
  37. //show gallery only if attachments exist
  38. if ($attachments) {
  39.  
  40. $output .= '<article class="gallery-wrap grid-container clearfix">';
  41.  
  42. //start attachment loop
  43. foreach ($attachments as $attachment) :
  44.  
  45.  
  46.  
  47. //featured image
  48. ($image_height) ? $image_attach_height = 450 * str_replace('%', '', $image_height) / 100 : $image_attach_height = 450;
  49. $thumb = $attachment->ID;
  50. $img_url = wp_get_attachment_url($thumb,'full'); //get full URL to image
  51. $featured_image = aq_resize( $img_url, 450, $image_attach_height, true ); //resize & crop the image
  52.  
  53. //output image
  54. $output .='<div class="gallery-photo grid-'.$columns.'">';
  55. $output .= '<a href="'. $img_url .'" class="single-gallery-thumb"';
  56. if($lightbox !='no'){
  57. $output .= 'rel="prettyPhoto['.$rand_gallery.']"';
  58. }
  59. $output .= 'title="'. apply_filters('the_title', $attachment->post_title) .'"><img src="'. $featured_image .'" alt="'. apply_filters('the_title', $attachment->post_title) .'" class="attachment-post-thumbnail" /></a>';
  60. $output .='</div>';
  61.  
  62. //end loop
  63. endforeach;
  64. $output .='</article>';
  65.  
  66. }
  67. return $output;
  68.  
  69. }
  70.  
  71. add_shortcode( 'wpex_gallery', 'wpex_gallery_shortcode' );
Advertisement
Add Comment
Please, Sign In to add comment