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

gallery shortcode/single image/numbers

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