Advertisement
alchymyth

gallery show image title

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