Advertisement
alchymyth

numbered gallery wp3.7.1 adjustment

Nov 19th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.27 KB | None | 0 0
  1. remove_shortcode('gallery','gallery_shortcode');
  2.  
  3. add_shortcode('gallery','custom_gallery_shortcode');
  4.  
  5. function custom_gallery_shortcode( $attr ) {
  6. /**
  7.  * The 'custom' Gallery shortcode.
  8.  *
  9.  * based on the code from  /wp-includes/media.php line 689 in version wp3.7.1
  10.  */
  11.     $post = get_post();
  12.  
  13.     static $instance = 0;
  14.     $instance++;
  15.  
  16.     if ( ! empty( $attr['ids'] ) ) {
  17.         // 'ids' is explicitly ordered, unless you specify otherwise.
  18.         if ( empty( $attr['orderby'] ) )
  19.             $attr['orderby'] = 'post__in';
  20.         $attr['include'] = $attr['ids'];
  21.     }
  22.  
  23.     // Allow plugins/themes to override the default gallery template.
  24.     $output = apply_filters('post_gallery', '', $attr);
  25.     if ( $output != '' )
  26.         return $output;
  27.    
  28.     // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  29.     if ( isset( $attr['orderby'] ) ) {
  30.         $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  31.         if ( !$attr['orderby'] )
  32.             unset( $attr['orderby'] );
  33.     }
  34.  
  35.     extract(shortcode_atts(array(
  36.         'order'      => 'ASC',
  37.         'orderby'    => 'menu_order ID',
  38.         'id'         => $post ? $post->ID : 0,
  39.         'itemtag'    => 'dl',
  40.         'icontag'    => 'dt',
  41.         'captiontag' => 'dd',
  42.         'columns'    => 3,
  43.         'size'       => 'thumbnail',
  44.         'include'    => '',
  45.         'exclude'    => '',
  46.         'link'       => ''
  47.     ), $attr, 'gallery'));
  48.  
  49.     $id = intval($id);
  50.     if ( 'RAND' == $order )
  51.         $orderby = 'none';
  52.  
  53.     if ( !empty($include) ) {
  54.         $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  55.  
  56.         $attachments = array();
  57.         foreach ( $_attachments as $key => $val ) {
  58.             $attachments[$val->ID] = $_attachments[$key];
  59.         }
  60.     } elseif ( !empty($exclude) ) {
  61.         $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  62.     } else {
  63.         $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  64.     }
  65.  
  66.     if ( empty($attachments) )
  67.         return '';
  68.  
  69.     if ( is_feed() ) {
  70.         $output = "\n";
  71.         foreach ( $attachments as $att_id => $attachment )
  72.             $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  73.         return $output;
  74.     }
  75.  
  76.     $itemtag = tag_escape($itemtag);
  77.     $captiontag = tag_escape($captiontag);
  78.     $columns = intval($columns);
  79.     $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  80.     $float = is_rtl() ? 'right' : 'left';
  81.  
  82.     $selector = "gallery-{$instance}";
  83.  
  84.     $gallery_style = $gallery_div = '';
  85.     if ( apply_filters( 'use_default_gallery_style', true ) )
  86.         $gallery_style = "
  87.         <style type='text/css'>
  88.             #{$selector} {
  89.                 margin: auto;
  90.             }
  91.             #{$selector} .gallery-item {
  92.                 float: {$float};
  93.                 margin-top: 10px;
  94.                 text-align: center;
  95.                 width: {$itemwidth}%;
  96.             }
  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.     $size_class = sanitize_html_class( $size );
  106.     $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  107.     $output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
  108.  
  109.     $i = 0;
  110.     foreach ( $attachments as $id => $attachment ) {
  111.         $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
  112.  
  113.         $output .= "<{$itemtag} class='gallery-item'>";
  114.        
  115.  $output .= "<div class='imageNum'><span>".($i+1)."</span></div>"; //NEW
  116.  
  117.         $output .= "
  118.             <{$icontag} class='gallery-icon'>
  119.                 $link
  120.             </{$icontag}>";
  121.         if ( $captiontag && trim($attachment->post_excerpt) ) {
  122.             $output .= "
  123.                 <{$captiontag} class='wp-caption-text gallery-caption'>
  124.                 " . wptexturize($attachment->post_excerpt) . "
  125.                 </{$captiontag}>";
  126.         }
  127.         $output .= "</{$itemtag}>";
  128.         if ( $columns > 0 && ++$i % $columns == 0 )
  129.             $output .= '<br style="clear: both" />';
  130.     }
  131.  
  132.     $output .= "
  133.             <br style='clear: both;' />
  134.         </div>\n";
  135.  
  136.     return $output;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement