Advertisement
alchymyth

fancybox gallery

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