Advertisement
Guest User

custom gallery

a guest
Jun 6th, 2011
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.31 KB | None | 0 0
  1. // Post Thumbnails
  2. add_theme_support( 'post-thumbnails' );
  3. set_post_thumbnail_size( 210, 102, true );
  4. add_image_size( 'gallery-size', 680, 380, true );
  5.  
  6. // Custom Gallery shortcode
  7.     add_shortcode('gallery_home', 'homepg_gallery_shortcode');
  8.    
  9.     function homepg_gallery_shortcode($attr) {
  10.         global $post;
  11.          
  12.         static $instance = 0;
  13.         $instance++;
  14.          
  15.         // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  16.         if ( isset( $attr['orderby'] ) ) {
  17.             $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  18.         if ( !$attr['orderby'] )
  19.             unset( $attr['orderby'] );
  20.         }
  21.          
  22.         extract(shortcode_atts(array(
  23.             'order' => 'ASC',
  24.             'orderby' => 'menu_order ID',
  25.             'id' => $post->ID,
  26.             'itemtag' => 'dl',
  27.             'icontag' => 'dt',
  28.             'captiontag' => 'dd',
  29.             'columns' => 3,
  30.             'size' => 'gallery-size',
  31.             'include' => '',
  32.             'exclude' => ''
  33.         ), $attr));
  34.          
  35.         $id = intval($id);
  36.         if ( 'RAND' == $order )
  37.         $orderby = 'none';
  38.          
  39.         if ( !empty($include) ) {
  40.             $include = preg_replace( '/[^0-9,]+/', '', $include );
  41.             $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  42.          
  43.             $attachments = array();
  44.             foreach ( $_attachments as $key => $val ) {
  45.                 $attachments[$val->ID] = $_attachments[$key];
  46.             }
  47.         } elseif ( !empty($exclude) ) {
  48.             $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  49.             $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  50.         } else {
  51.             $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  52.         }
  53.          
  54.         if ( empty($attachments) )
  55.             return '';
  56.          
  57.         if ( is_feed() ) {
  58.             $output = "";
  59.             foreach ( $attachments as $att_id => $attachment )
  60.             $output .= wp_get_attachment_link($att_id, $size, true) . "";
  61.             return $output;
  62.         }
  63.          
  64.         // check to see if tags have been set to false. If they are the defaults or have been set to a string value use that as the tag.
  65.         if ($itemtag) $itemtag = tag_escape($itemtag);
  66.         if ($captiontag) $captiontag = tag_escape($captiontag);
  67.         if ($icontag) $icontag = tag_escape($icontag);
  68.         $columns = intval($columns);
  69.          
  70.         $selector = "gallery-{$instance}";
  71.          
  72.         $output = "";
  73.          
  74.         $i = 0;
  75.         foreach ( $attachments as $id => $attachment ) {
  76.         ++$i;
  77.         $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_image($id, $size, false) : wp_get_attachment_image($id, $size, false);
  78.          
  79.         if ($itemtag) {
  80.             $output .= '';
  81.         if( $columns > 0 && $i % $columns == 0 ) $output .= "";
  82.             $output .= '';
  83.         }
  84.         if ($icontag) $output .= "";
  85.             $output .= "".$link;
  86.         if ($icontag) $output .= "";
  87.        
  88.         // if the attachment has a caption set
  89.         if ( trim($attachment->post_excerpt) ) {
  90.             if ($captiontag) $output .= "";
  91.                 $output .= wptexturize($attachment->post_excerpt);
  92.             if ($captiontag) $output .= "";
  93.         }
  94.         if ($itemtag) $output .= "";
  95.             if ( $columns > 0 && $i % $columns == 0 ) $output .= "";
  96.         }
  97.          
  98.         $output .= "";
  99.          
  100.         return $output;
  101.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement