FireBot

wordpress-gallery-shortcode

Mar 3rd, 2012
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.62 KB | None | 0 0
  1. <?php
  2. //Gallery Shortcode change for home page slider
  3. remove_shortcode('gallery', 'gallery_shortcode');
  4. add_shortcode('gallery', 'gallery_shortcode_slider');
  5.  
  6. function gallery_shortcode_slider($attr) {
  7.     global $post, $wp_locale;
  8.  
  9.     static $instance = 0;
  10.    
  11.     $instance++;
  12.  
  13.     // Allow plugins/themes to override the default gallery template.
  14.     $output = apply_filters('post_gallery', '', $attr);
  15.     if ( $output != '' )
  16.         return $output;
  17.  
  18.     // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  19.     if ( isset( $attr['orderby'] ) ) {
  20.         $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  21.         if ( !$attr['orderby'] )
  22.             unset( $attr['orderby'] );
  23.     }
  24.  
  25.     extract(shortcode_atts(array(
  26.         'order'      => 'ASC',
  27.         'orderby'    => 'menu_order ID',
  28.         'id'         => $post->ID,
  29.         'itemtag'    => 'dl',
  30.         'icontag'    => 'dt',
  31.         'captiontag' => 'dd',
  32.         'columns'    => 3,
  33.         'size'       => 'thumbnail',
  34.         'include'    => '',
  35.         'exclude'    => ''
  36.     ), $attr));
  37.  
  38.     $id = intval($id);
  39.     if ( 'RAND' == $order )
  40.         $orderby = 'none';
  41.  
  42.     if ( !empty($include) ) {
  43.         $include = preg_replace( '/[^0-9,]+/', '', $include );
  44.         $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  45.  
  46.         $attachments = array();
  47.         foreach ( $_attachments as $key => $val ) {
  48.             $attachments[$val->ID] = $_attachments[$key];
  49.         }
  50.     } elseif ( !empty($exclude) ) {
  51.         $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  52.         $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  53.     } else {
  54.         $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  55.     }
  56.  
  57.     if ( empty($attachments) )
  58.         return '';
  59.  
  60.     if ( is_feed() ) {
  61.         $output = "\n";
  62.         foreach ( $attachments as $att_id => $attachment )
  63.             $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  64.         return $output;
  65.     }
  66.  
  67.     $itemtag = tag_escape($itemtag);
  68.     $captiontag = tag_escape($captiontag);
  69.     $columns = intval($columns);
  70.     $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  71.     $float = is_rtl() ? 'right' : 'left';
  72.  
  73.     $selector = "gallery-{$instance}";
  74.  
  75.     $output = apply_filters('gallery_style', "
  76.         <style type='text/css'>
  77.             #{$selector} {
  78.                 margin: auto;
  79.             }
  80.             #{$selector} .gallery-item {
  81.                 float: {$float};
  82.                 margin-top: 10px;
  83.                 text-align: center;
  84.                 width: {$itemwidth}%;           }
  85.             #{$selector} img {
  86.                 border: 2px solid #cfcfcf;
  87.             }
  88.             #{$selector} .gallery-caption {
  89.                 margin-left: 0;
  90.             }
  91.         </style>
  92.         <!-- see gallery_shortcode() in wp-includes/media.php -->
  93.         <div id='$selector' class='gallery galleryid-{$id}'>");
  94.  
  95.     $i = 0;
  96.     foreach ( $attachments as $id => $attachment ) {
  97.         $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
  98.  
  99.         $output .= "<{$itemtag}>";
  100.         $output .= "
  101.             <{$icontag}>
  102.                 $link
  103.             </{$icontag}>";
  104.         if ( $captiontag && trim($attachment->post_excerpt) ) {
  105.             $output .= "
  106.                 <{$captiontag}>
  107.                 " . wptexturize($attachment->post_excerpt) . "
  108.                 </{$captiontag}>";
  109.         }
  110.         $output .= "</{$itemtag}>";
  111.         if ( $columns > 0 && ++$i % $columns == 0 )
  112.             $output .= '<br style="clear: both" />';
  113.     }
  114.  
  115.             "<br style='clear: both;' />
  116.         </div>\n";
  117.  
  118.     return $output;
  119. }
  120. ?>
Advertisement
Add Comment
Please, Sign In to add comment