Permalink
Browse files

Replacement gallery shortcut function

  • Loading branch information...
1 parent 57e2fc5 commit cff16f0a323e69f5cdecfa861fe7fd1ca287fdec @splorp committed Jan 24, 2012
Showing with 103 additions and 0 deletions.
  1. +103 −0 functions.php
View
@@ -252,6 +252,109 @@ function decruft_avatars($str) {
add_filter ('get_avatar','decruft_avatars');
+// Replacement gallery shortcut function
+// Removes default cruft and verbosity
+
+remove_shortcode('gallery');
+add_shortcode('gallery', 'tersus_gallery');
+
+// Portions by Michael Preuss and Aaron Cimolini
+// http://snipplr.com/view.php?codeview&id=27051
+
+function tersus_gallery($attr) {
+ global $post;
+
+ static $instance = 0;
+ $instance++;
+
+ // Check for a valid orderby statement
+ if ( isset( $attr['orderby'] ) ) {
+ $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
+ if ( !$attr['orderby'] )
+ unset( $attr['orderby'] );
+ }
+
+ extract(shortcode_atts(array(
+ 'order' => 'ASC',
+ 'orderby' => 'menu_order ID',
+ 'id' => $post->ID,
+ 'itemtag' => 'dl',
+ 'icontag' => 'dt',
+ 'captiontag' => 'dd',
+ 'columns' => 3,
+ 'size' => 'thumbnail',
+ 'include' => '',
+ 'exclude' => ''
+ ), $attr));
+
+ $id = intval($id);
+ if ( 'RAND' == $order )
+ $orderby = 'none';
+
+ if ( !empty($include) ) {
+ $include = preg_replace( '/[^0-9,]+/', '', $include );
+ $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
+
+ $attachments = array();
+ foreach ( $_attachments as $key => $val ) {
+ $attachments[$val->ID] = $_attachments[$key];
+ }
+ } elseif ( !empty($exclude) ) {
+ $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
+ $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
+ } else {
+ $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
+ }
+
+ if ( empty($attachments) )
+ return '';
+
+ if ( is_feed() ) {
+ $output = "\n";
+ foreach ( $attachments as $att_id => $attachment )
+ $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
+ return $output;
+ }
+
+ // Check to see whether any tags have been set to false
+ if ($itemtag) $itemtag = tag_escape($itemtag);
+ if ($captiontag) $captiontag = tag_escape($captiontag);
+ if ($icontag) $icontag = tag_escape($icontag);
+ $columns = intval($columns);
+
+ $selector = "gallery-{$instance}";
+
+ $output = "<div id='$selector'>\n";
+
+ $i = 0;
+ foreach ( $attachments as $id => $attachment ) {
+ ++$i;
+ $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
+
+ if ($itemtag) {
+ $output .= '<'.$itemtag;
+ if( $columns > 0 && $i % $columns == 0 ) $output .= " last";
+ $output .= '">';
+ }
+ if ($icontag) $output .= "\n\t<" .$icontag. ">\t";
+ $output .= "\n\t".$link;
+ if ($icontag) $output .= "\n\t</".$icontag.">";
+ // if the attachment has a caption set
+ if ( trim($attachment->post_excerpt) ) {
+ if ($captiontag) $output .= "\n<" .$captiontag. ">\n\t";
+ $output .= wptexturize($attachment->post_excerpt);
+ if ($captiontag) $output .= "\n</" .$captiontag. ">" . "\n";
+ }
+ if ($itemtag) $output .= "\n</".$itemtag .">\n";
+ if ( $columns > 0 && $i % $columns == 0 ) $output .= "\n";
+ }
+
+ $output .= "</div>\n";
+
+ return $output;
+}
+
+
// Replacement comment callback function
// Removes default class and ID verbosity

0 comments on commit cff16f0

Please sign in to comment.