Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

custom gallery shortcode

By: alchymyth on Oct 11th, 2012  |  syntax: PHP  |  size: 4.67 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. remove_shortcode('gallery','gallery_shortcode');
  2.  
  3. add_shortcode('gallery','custom_gallery_shortcode');
  4.  
  5.  
  6. function custom_gallery_shortcode( $attr ) {
  7. /**
  8.  * The 'custom' Gallery shortcode.
  9.  * 2012 oct by http://wordpress.org/support/profile/alchymyth
  10.  *
  11.  * to add consecutive numbers to gallery images;
  12.  * and to show only one image on the front (index) page with a link to the single post.
  13.  *
  14.  * based on the code from  /wp-includes/media.php line 745 in version wp3.2
  15.  */
  16.         global $post;
  17.  
  18.         static $instance = 0;
  19.         $instance++;
  20.        
  21.         // Allow plugins/themes to override the default gallery template.
  22.         $output = apply_filters('post_gallery', '', $attr);
  23.         if ( $output != '' )
  24.                 return $output;
  25.  
  26.         // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  27.         if ( isset( $attr['orderby'] ) ) {
  28.                 $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  29.                 if ( !$attr['orderby'] )
  30.                         unset( $attr['orderby'] );
  31.         }
  32.  
  33.         extract(shortcode_atts(array(
  34.                 'order'      => 'ASC',
  35.                 'orderby'    => 'menu_order ID',
  36.                 'id'         => $post->ID,
  37.                 'itemtag'    => 'dl',
  38.                 'icontag'    => 'dt',
  39.                 'captiontag' => 'dd',
  40.                 'columns'    => 3,
  41.                 'size'       => 'thumbnail',
  42.                 'include'    => '',
  43.                 'exclude'    => ''
  44.         ), $attr));
  45.  
  46.         $id = intval($id);
  47.         if ( 'RAND' == $order )
  48.                 $orderby = 'none';
  49.  
  50.         if ( !empty($include) ) {
  51.                 $include = preg_replace( '/[^0-9,]+/', '', $include );
  52.                 $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  53.  
  54.                 $attachments = array();
  55.                 foreach ( $_attachments as $key => $val ) {
  56.                         $attachments[$val->ID] = $_attachments[$key];
  57.                 }
  58.         } elseif ( !empty($exclude) ) {
  59.                 $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  60.                 $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  61.         } else {
  62.                 $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  63.         }
  64.  
  65.         if ( empty($attachments) )
  66.                 return '';
  67.  
  68.         if ( is_feed() ) {
  69.                 $output = "\n";
  70.                 foreach ( $attachments as $att_id => $attachment )
  71.                         $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  72.                 return $output;
  73.         }
  74.  
  75.         $itemtag = tag_escape($itemtag);
  76.         $captiontag = tag_escape($captiontag);
  77.         $columns = intval($columns);
  78.         $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  79.         $float = is_rtl() ? 'right' : 'left';
  80.  
  81.         $selector = "gallery-{$instance}";
  82.  
  83.         $gallery_style = $gallery_div = '';
  84.         if ( apply_filters( 'use_default_gallery_style', true ) )
  85.                 $gallery_style = "
  86.                 <style type='text/css'>
  87.                         #{$selector} {
  88.                                 margin: auto;
  89.                         }
  90.                         #{$selector} .gallery-item {
  91.                                 float: {$float};
  92.                                 margin-top: 10px;
  93.                                 text-align: center;
  94.                                 width: {$itemwidth}%;
  95.                         }
  96.                         #{$selector} img {
  97.                                 border: 2px solid #cfcfcf;
  98.                         }
  99.                         #{$selector} .gallery-caption {
  100.                                 margin-left: 0;
  101.                         }
  102.                 </style>
  103.                 <!-- see gallery_shortcode() in wp-includes/media.php -->";
  104.         $size_class = sanitize_html_class( $size );
  105.         $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  106.         $output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
  107.  
  108.         $i = 0;
  109.         foreach ( $attachments as $id => $attachment ) {
  110.                 $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
  111.  
  112.                 $output .= "<{$itemtag} class='gallery-item'>";
  113.                
  114.                 $output .= "
  115.                         <{$icontag} class='gallery-icon'><span class='imageNum'><span>".($i+1)."</span></span>
  116.                                 $link
  117.                         </{$icontag}>";
  118.                 if ( $captiontag && trim($attachment->post_excerpt) ) {
  119.                         $output .= "
  120.                                 <{$captiontag} class='wp-caption-text gallery-caption'>
  121.                                 " . wptexturize($attachment->post_excerpt) . "
  122.                                 </{$captiontag}>";
  123.                 }
  124.                 $output .= "</{$itemtag}>";
  125.                 if ( $columns > 0 && ++$i % $columns == 0 )
  126.                         $output .= '<br style="clear: both" />';
  127.  
  128. //break the foreach loop on index pages
  129. if( !is_singular() ) break;
  130. //breaks the foreach loop on index pages       
  131.  
  132. }
  133.  
  134.         $output .= "
  135.                         <br style='clear: both;' />";
  136.                                                
  137.         $output .= "    </div>\n";
  138.        
  139. //add 'read-more' on index pages
  140. if( !is_singular() ) $output .= "<p style='text-align:center;width:100%;'><a href='" . get_permalink($post->ID) . "#" . $post->ID . "'>View Full Gallery</a></p>";
  141. //adds the 'read more' on index pages
  142.  
  143.         return $output;
  144. }