Advertisement
alchymyth

custom numbered gallery shortcode wp4.9

Jan 10th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.50 KB | None | 0 0
  1. // START custom gallery code for images with numbers//
  2.  
  3. remove_shortcode('gallery','gallery_shortcode');
  4.  
  5. add_shortcode('gallery','custom_gallery_shortcode');
  6.  
  7. function custom_gallery_shortcode( $attr ) {
  8. /**
  9.  * The 'custom' Gallery shortcode.
  10.  *
  11.  * based on the code from  /wp-includes/media.php line 689 in version wp3.7.1
  12.  */
  13.     $post = get_post();
  14.  
  15.     static $instance = 0;
  16.     $instance++;
  17.  
  18.     if ( ! empty( $attr['ids'] ) ) {
  19.         // 'ids' is explicitly ordered, unless you specify otherwise.
  20.         if ( empty( $attr['orderby'] ) ) {
  21.             $attr['orderby'] = 'post__in';
  22.         }
  23.         $attr['include'] = $attr['ids'];
  24.     }
  25.  
  26.     /**
  27.      * Filters the default gallery shortcode output.
  28.      *
  29.      * If the filtered output isn't empty, it will be used instead of generating
  30.      * the default gallery template.
  31.      *
  32.      * @since 2.5.0
  33.      * @since 4.2.0 The `$instance` parameter was added.
  34.      *
  35.      * @see gallery_shortcode()
  36.      *
  37.      * @param string $output   The gallery output. Default empty.
  38.      * @param array  $attr     Attributes of the gallery shortcode.
  39.      * @param int    $instance Unique numeric ID of this gallery shortcode instance.
  40.      */
  41.     $output = apply_filters( 'post_gallery', '', $attr, $instance );
  42.     if ( $output != '' ) {
  43.         return $output;
  44.     }
  45.  
  46.     $html5 = current_theme_supports( 'html5', 'gallery' );
  47.     $atts = shortcode_atts( array(
  48.         'order'      => 'ASC',
  49.         'orderby'    => 'menu_order ID',
  50.         'id'         => $post ? $post->ID : 0,
  51.         'itemtag'    => $html5 ? 'figure'     : 'dl',
  52.         'icontag'    => $html5 ? 'div'        : 'dt',
  53.         'captiontag' => $html5 ? 'figcaption' : 'dd',
  54.         'columns'    => 3,
  55.         'size'       => 'thumbnail',
  56.         'include'    => '',
  57.         'exclude'    => '',
  58.         'link'       => ''
  59.     ), $attr, 'gallery' );
  60.  
  61.     $id = intval( $atts['id'] );
  62.  
  63.     if ( ! empty( $atts['include'] ) ) {
  64.         $_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
  65.  
  66.         $attachments = array();
  67.         foreach ( $_attachments as $key => $val ) {
  68.             $attachments[$val->ID] = $_attachments[$key];
  69.         }
  70.     } elseif ( ! empty( $atts['exclude'] ) ) {
  71.         $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
  72.     } else {
  73.         $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
  74.     }
  75.  
  76.     if ( empty( $attachments ) ) {
  77.         return '';
  78.     }
  79.  
  80.     if ( is_feed() ) {
  81.         $output = "\n";
  82.         foreach ( $attachments as $att_id => $attachment ) {
  83.             $output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n";
  84.         }
  85.         return $output;
  86.     }
  87.  
  88. $j = 1; //new for numbering
  89.  
  90.     $itemtag = tag_escape( $atts['itemtag'] );
  91.     $captiontag = tag_escape( $atts['captiontag'] );
  92.     $icontag = tag_escape( $atts['icontag'] );
  93.     $valid_tags = wp_kses_allowed_html( 'post' );
  94.     if ( ! isset( $valid_tags[ $itemtag ] ) ) {
  95.         $itemtag = 'dl';
  96.     }
  97.     if ( ! isset( $valid_tags[ $captiontag ] ) ) {
  98.         $captiontag = 'dd';
  99.     }
  100.     if ( ! isset( $valid_tags[ $icontag ] ) ) {
  101.         $icontag = 'dt';
  102.     }
  103.  
  104.     $columns = intval( $atts['columns'] );
  105.     $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  106.     $float = is_rtl() ? 'right' : 'left';
  107.  
  108.     $selector = "gallery-{$instance}";
  109.  
  110.     $gallery_style = '';
  111.  
  112.     /**
  113.      * Filters whether to print default gallery styles.
  114.      *
  115.      * @since 3.1.0
  116.      *
  117.      * @param bool $print Whether to print default gallery styles.
  118.      *                    Defaults to false if the theme supports HTML5 galleries.
  119.      *                    Otherwise, defaults to true.
  120.      */
  121.     if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) {
  122.         $gallery_style = "
  123.         <style type='text/css'>
  124.             #{$selector} {
  125.                 margin: auto;
  126.             }
  127.             #{$selector} .gallery-item {
  128.                 float: {$float};
  129.                 margin-top: 10px;
  130.                 text-align: center;
  131.                 width: {$itemwidth}%;
  132.             }
  133.             #{$selector} img {
  134.                 border: 2px solid #cfcfcf;
  135.             }
  136.             #{$selector} .gallery-caption {
  137.                 margin-left: 0;
  138.             }
  139.             /* see gallery_shortcode() in wp-includes/media.php */
  140.         </style>\n\t\t";
  141.     }
  142.  
  143.     $size_class = sanitize_html_class( $atts['size'] );
  144.     $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  145.  
  146.     /**
  147.      * Filters the default gallery shortcode CSS styles.
  148.      *
  149.      * @since 2.5.0
  150.      *
  151.      * @param string $gallery_style Default CSS styles and opening HTML div container
  152.      *                              for the gallery shortcode output.
  153.      */
  154.     $output = apply_filters( 'gallery_style', $gallery_style . $gallery_div );
  155.  
  156.     $i = 0;
  157.    
  158.     foreach ( $attachments as $id => $attachment ) {
  159.  
  160.         $attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : '';
  161.         if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) {
  162.             $image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr );
  163.         } elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) {
  164.             $image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr );
  165.         } else {
  166.             $image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr );
  167.         }
  168.         $image_meta  = wp_get_attachment_metadata( $id );
  169.  
  170.         $orientation = '';
  171.         if ( isset( $image_meta['height'], $image_meta['width'] ) ) {
  172.             $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
  173.         }
  174.         $output .= "<{$itemtag} class='gallery-item'>";
  175.        
  176.  $output .= "<div class='gallery-image-number'><span>".($j++)."</span></div>"; //NEW for numbering
  177.        
  178.         $output .= "
  179.             <{$icontag} class='gallery-icon {$orientation}'>
  180.                 $image_output
  181.             </{$icontag}>";
  182.         if ( $captiontag && trim($attachment->post_excerpt) ) {
  183.             $output .= "
  184.                 <{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'>
  185.                 " . wptexturize($attachment->post_excerpt) . "
  186.                 </{$captiontag}>";
  187.         }
  188.         $output .= "</{$itemtag}>";
  189.         if ( ! $html5 && $columns > 0 && ++$i % $columns == 0 ) {
  190.             $output .= '<br style="clear: both" />';
  191.         }
  192.     }
  193.  
  194.     if ( ! $html5 && $columns > 0 && $i % $columns !== 0 ) {
  195.         $output .= "
  196.             <br style='clear: both' />";
  197.     }
  198.  
  199.     $output .= "
  200.         </div>\n";
  201.  
  202.     return $output;
  203. }
  204.  
  205. // CSS needed to format the number:
  206. // .gallery-image-number
  207.  
  208. // END custom gallery code for images with numbers//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement