| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Alternate Gallery Shortcode |
|---|
| 4 | Plugin URI: http://davidtorbert.com |
|---|
| 5 | Description: Allows the gallery to be limited to a certain number of items. This allows you to have a parent gallery page, which will link to the child galleries. Note that this function is just a block copy of the gallery_shortcode() function from Wordpress with the addition of the maximum option to the [gallery] shortcode. If/when Wordpress updates this function, we will need to update our's. |
|---|
| 6 | Version: 1.0 |
|---|
| 7 | Author: David Torbert |
|---|
| 8 | Author URI: http://davidtorbert.com |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * The Gallery shortcode. |
|---|
| 14 | * |
|---|
| 15 | * This implements the functionality of the Gallery Shortcode for displaying |
|---|
| 16 | * WordPress images on a post. |
|---|
| 17 | * |
|---|
| 18 | * @since 2.5.0 |
|---|
| 19 | * |
|---|
| 20 | * @param array $attr Attributes attributed to the shortcode. |
|---|
| 21 | * @return string HTML content to display gallery. |
|---|
| 22 | */ |
|---|
| 23 | function alt_gallery_shortcode($null, $attr = array()) { |
|---|
| 24 | global $post, $wp_locale; |
|---|
| 25 | |
|---|
| 26 | static $instance = 0; |
|---|
| 27 | $instance++; |
|---|
| 28 | |
|---|
| 29 | // We're trusting author input, so let's at least make sure it looks like a valid orderby statement |
|---|
| 30 | if ( isset( $attr['orderby'] ) ) { |
|---|
| 31 | $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); |
|---|
| 32 | if ( !$attr['orderby'] ) |
|---|
| 33 | unset( $attr['orderby'] ); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | extract(shortcode_atts(array( |
|---|
| 37 | 'order' => 'ASC', |
|---|
| 38 | 'orderby' => 'menu_order ID', |
|---|
| 39 | 'id' => $post->ID, |
|---|
| 40 | 'itemtag' => 'dl', |
|---|
| 41 | 'icontag' => 'dt', |
|---|
| 42 | 'captiontag' => 'dd', |
|---|
| 43 | 'columns' => 3, |
|---|
| 44 | 'size' => 'thumbnail', |
|---|
| 45 | 'include' => '', |
|---|
| 46 | 'exclude' => '', |
|---|
| 47 | 'maximum' => 0 |
|---|
| 48 | ), $attr)); |
|---|
| 49 | |
|---|
| 50 | $id = intval($id); |
|---|
| 51 | if ( 'RAND' == $order ) |
|---|
| 52 | $orderby = 'none'; |
|---|
| 53 | |
|---|
| 54 | if ( !empty($include) ) { |
|---|
| 55 | $include = preg_replace( '/[^0-9,]+/', '', $include ); |
|---|
| 56 | $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); |
|---|
| 57 | |
|---|
| 58 | $attachments = array(); |
|---|
| 59 | foreach ( $_attachments as $key => $val ) { |
|---|
| 60 | $attachments[$val->ID] = $_attachments[$key]; |
|---|
| 61 | } |
|---|
| 62 | } elseif ( !empty($exclude) ) { |
|---|
| 63 | $exclude = preg_replace( '/[^0-9,]+/', '', $exclude ); |
|---|
| 64 | $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); |
|---|
| 65 | } else { |
|---|
| 66 | $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | if ( empty($attachments) ) |
|---|
| 70 | return ''; |
|---|
| 71 | |
|---|
| 72 | if ( is_feed() ) { |
|---|
| 73 | $output = "\n"; |
|---|
| 74 | foreach ( $attachments as $att_id => $attachment ) |
|---|
| 75 | $output .= wp_get_attachment_link($att_id, $size, true) . "\n"; |
|---|
| 76 | return $output; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | $itemtag = tag_escape($itemtag); |
|---|
| 80 | $captiontag = tag_escape($captiontag); |
|---|
| 81 | $columns = intval($columns); |
|---|
| 82 | $itemwidth = $columns > 0 ? floor(100/$columns) : 100; |
|---|
| 83 | $float = $wp_locale->text_direction == 'rtl' ? 'right' : 'left'; |
|---|
| 84 | |
|---|
| 85 | $selector = "gallery-{$instance}"; |
|---|
| 86 | |
|---|
| 87 | $output = apply_filters('gallery_style', " |
|---|
| 88 | <style type='text/css'> |
|---|
| 89 | #{$selector} { |
|---|
| 90 | margin: auto; |
|---|
| 91 | } |
|---|
| 92 | #{$selector} .gallery-item { |
|---|
| 93 | float: {$float}; |
|---|
| 94 | margin-top: 10px; |
|---|
| 95 | text-align: center; |
|---|
| 96 | width: {$itemwidth}%; } |
|---|
| 97 | #{$selector} img { |
|---|
| 98 | border: 2px solid #cfcfcf; |
|---|
| 99 | } |
|---|
| 100 | #{$selector} .gallery-caption { |
|---|
| 101 | margin-left: 0; |
|---|
| 102 | } |
|---|
| 103 | </style> |
|---|
| 104 | <!-- see gallery_shortcode() in wp-includes/media.php --> |
|---|
| 105 | <div id='$selector' class='gallery galleryid-{$id}'>"); |
|---|
| 106 | |
|---|
| 107 | $i = 0; |
|---|
| 108 | foreach ( $attachments as $id => $attachment ) { |
|---|
| 109 | $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false); |
|---|
| 110 | |
|---|
| 111 | $output .= "<{$itemtag} class='gallery-item'>"; |
|---|
| 112 | $output .= " |
|---|
| 113 | <{$icontag} class='gallery-icon'> |
|---|
| 114 | $link |
|---|
| 115 | </{$icontag}>"; |
|---|
| 116 | if ( $captiontag && trim($attachment->post_excerpt) ) { |
|---|
| 117 | $output .= " |
|---|
| 118 | <{$captiontag} class='gallery-caption'> |
|---|
| 119 | " . wptexturize($attachment->post_excerpt) . " |
|---|
| 120 | </{$captiontag}>"; |
|---|
| 121 | } |
|---|
| 122 | $output .= "</{$itemtag}>"; |
|---|
| 123 | if ( $columns > 0 && ++$i % $columns == 0 ) |
|---|
| 124 | $output .= '<br style="clear: both" />'; |
|---|
| 125 | if (($maximum != 0 && ($i >= $maximum)) |
|---|
| 126 | { |
|---|
| 127 | break; |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | $output .= " |
|---|
| 132 | <br style='clear: both;' /> |
|---|
| 133 | </div>\n"; |
|---|
| 134 | |
|---|
| 135 | return $output; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | add_filter('post_gallery', 'alt_gallery_shortcode', 10, 2); |
|---|
| 139 | |
|---|
| 140 | ?> |
|---|