Advertisement
Guest User

Untitled

a guest
Jan 19th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. add_shortcode('custom_gallery', 'gallery_custom_shortcode');
  2. function gallery_custom_shortcode( $attr ) {
  3. $post = get_post();
  4.  
  5. static $instance = 0;
  6. $instance++;
  7.  
  8. if ( ! empty( $attr['ids'] ) ) {
  9. // 'ids' is explicitly ordered, unless you specify otherwise.
  10. if ( empty( $attr['orderby'] ) ) {
  11. $attr['orderby'] = 'post__in';
  12. }
  13. $attr['include'] = $attr['ids'];
  14. }
  15.  
  16. /**
  17. * Filter the default gallery shortcode output.
  18. *
  19. * If the filtered output isn't empty, it will be used instead of generating
  20. * the default gallery template.
  21. *
  22. * @since 2.5.0
  23. *
  24. * @see gallery_shortcode()
  25. *
  26. * @param string $output The gallery output. Default empty.
  27. * @param array $attr Attributes of the gallery shortcode.
  28. */
  29. $output = apply_filters( 'post_gallery', '', $attr );
  30. if ( $output != '' ) {
  31. return $output;
  32. }
  33.  
  34. $html5 = current_theme_supports( 'html5', 'gallery' );
  35. $atts = shortcode_atts( array(
  36. 'order' => 'ASC',
  37. 'orderby' => 'menu_order ID',
  38. 'id' => $post ? $post->ID : 0,
  39. 'itemtag' => $html5 ? 'figure' : 'dl',
  40. 'icontag' => $html5 ? 'div' : 'dt',
  41. 'captiontag' => $html5 ? 'figcaption' : 'dd',
  42. 'columns' => 3,
  43. 'size' => 'thumbnail',
  44. 'include' => '',
  45. 'exclude' => '',
  46. 'link' => ''
  47. ), $attr, 'gallery' );
  48.  
  49. $id = intval( $atts['id'] );
  50.  
  51. if ( ! empty( $atts['include'] ) ) {
  52. $_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
  53.  
  54. $attachments = array();
  55. foreach ( $_attachments as $key => $val ) {
  56. $attachments[$val->ID] = $_attachments[$key];
  57. }
  58. } elseif ( ! empty( $atts['exclude'] ) ) {
  59. $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
  60. } else {
  61. $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
  62. }
  63.  
  64. if ( empty( $attachments ) ) {
  65. return '';
  66. }
  67.  
  68. if ( is_feed() ) {
  69. $output = "\n";
  70. foreach ( $attachments as $att_id => $attachment ) {
  71. $output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n";
  72. }
  73. return $output;
  74. }
  75.  
  76. $itemtag = tag_escape( $atts['itemtag'] );
  77. $captiontag = tag_escape( $atts['captiontag'] );
  78. $icontag = tag_escape( $atts['icontag'] );
  79. $valid_tags = wp_kses_allowed_html( 'post' );
  80. if ( ! isset( $valid_tags[ $itemtag ] ) ) {
  81. $itemtag = 'dl';
  82. }
  83. if ( ! isset( $valid_tags[ $captiontag ] ) ) {
  84. $captiontag = 'dd';
  85. }
  86. if ( ! isset( $valid_tags[ $icontag ] ) ) {
  87. $icontag = 'dt';
  88. }
  89.  
  90. $columns = intval( $atts['columns'] );
  91. $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  92. $float = is_rtl() ? 'right' : 'left';
  93.  
  94. $selector = "gallery-{$instance}";
  95.  
  96. $gallery_style = '';
  97.  
  98. /**
  99. * Filter whether to print default gallery styles.
  100. *
  101. * @since 3.1.0
  102. *
  103. * @param bool $print Whether to print default gallery styles.
  104. * Defaults to false if the theme supports HTML5 galleries.
  105. * Otherwise, defaults to true.
  106. */
  107. if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) {
  108. $gallery_style = "
  109. <style type='text/css'>
  110. #{$selector} {
  111. margin: auto;
  112. }
  113. #{$selector} .gallery-item {
  114. float: {$float};
  115. margin-top: 10px;
  116. text-align: center;
  117. width: {$itemwidth}%;
  118. }
  119. #{$selector} img {
  120. border: 2px solid #cfcfcf;
  121. }
  122. #{$selector} .gallery-caption {
  123. margin-left: 0;
  124. }
  125. /* see gallery_shortcode() in wp-includes/media.php */
  126. </style>\n\t\t";
  127. }
  128.  
  129. $size_class = sanitize_html_class( $atts['size'] );
  130. $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  131.  
  132. /**
  133. * Filter the default gallery shortcode CSS styles.
  134. *
  135. * @since 2.5.0
  136. *
  137. * @param string $gallery_style Default CSS styles and opening HTML div container
  138. * for the gallery shortcode output.
  139. */
  140. $output = apply_filters( 'gallery_style', $gallery_style . $gallery_div );
  141.  
  142. $i = 0;
  143. foreach ( $attachments as $id => $attachment ) {
  144.  
  145. $attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : '';
  146. if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) {
  147. $image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr );
  148. } elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) {
  149. $image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr );
  150. } else {
  151. $image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr );
  152. }
  153. $image_meta = wp_get_attachment_metadata( $id );
  154.  
  155. $orientation = '';
  156. if ( isset( $image_meta['height'], $image_meta['width'] ) ) {
  157. $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
  158. }
  159. $output .= "<{$itemtag} class='gallery-item'>";
  160. $output .= "
  161. <{$icontag} class='gallery-icon {$orientation}'>
  162. $image_output
  163. </{$icontag}>";
  164. if ( $captiontag && trim($attachment->post_excerpt) ) {
  165. $output .= "
  166. <{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'>
  167. " . wptexturize($attachment->post_excerpt) . "
  168. </{$captiontag}>";
  169. }
  170. $output .= "</{$itemtag}>";
  171. if ( ! $html5 && $columns > 0 && ++$i % $columns == 0 ) {
  172. $output .= '<br style="clear: both" />';
  173. }
  174. }
  175.  
  176. if ( ! $html5 && $columns > 0 && $i % $columns !== 0 ) {
  177. $output .= "
  178. <br style='clear: both' />";
  179. }
  180.  
  181. $output .= "
  182. </div>\n";
  183.  
  184. return $output;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement