Advertisement
miriamdepaula

WordPress: Native Gallery Paginated

Aug 16th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.71 KB | None | 0 0
  1. function gallery_with_pagination($attr) {
  2.     global $post;
  3.  
  4.     static $instance = 0;
  5.     $instance++;
  6.  
  7.     extract( shortcode_atts( array(
  8.         'qty' => '5'
  9.     ), $attr ) );
  10.  
  11.     $imagesPerPage = $qty;
  12.  
  13.     // Define some default options
  14.     $options = array(
  15.         'order'=> 'ASC',
  16.         'orderby'=> 'menu_order ID',
  17.         'itemtag'=> 'dl',
  18.         'icontag'=> 'dt',
  19.         'captiontag'=> 'dd',
  20.         'columns'=> 3,
  21.         'size'=> 'thumbnail',
  22.         'perpage'=> $imagesPerPage,
  23.         'link'=>'attachment',
  24.         'show_edit_links'=>'Y',
  25.         'use_shortcode'=>'gallery',
  26.         'exclude'=>''
  27.     );
  28.  
  29.     // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  30.     if ( isset( $attr['orderby'] ) ) {
  31.         $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  32.         if ( !$attr['orderby'] )
  33.             unset( $attr['orderby'] );
  34.     }
  35.  
  36.     // Overwrite the defaults with any options passed in
  37.     if (is_array($attr)) $options = array_merge($options, $attr);
  38.  
  39.     // Start by getting the attachments
  40.     $attachments = get_children(array(
  41.         'post_parent'=> $post->ID,
  42.         'post_status'=>'inherit',
  43.         'post_type'=> 'attachment',
  44.         'post_mime_type'=>'image',
  45.         'order'=> $options['order'],
  46.         'orderby'=> $options['orderby'],
  47.         'exclude'=> $options['exclude']
  48.     ));
  49.  
  50.     // If we don't have any attachments - output nothing
  51.     if ( empty($attachments) ) return '';
  52.  
  53.     // Output feed if requested
  54.     if ( is_feed() ) {
  55.         $output = "\n";
  56.  
  57.         foreach ( $attachments as $id => $attachment )
  58.             $output .= wp_get_attachment_link($id, $options['size'], true) . "\n";
  59.         return $output;
  60.     }
  61.  
  62.     // Standard post output
  63.  
  64.     // Work out how many pages we need and what page we are currently on
  65.     $imageCount = count($attachments);
  66.     $pageCount = ceil($imageCount / $imagesPerPage);
  67.  
  68.     $currentPage = intval($_GET['galleryPage']);
  69.     if ( empty($currentPage) || $currentPage<=0 ) $currentPage=1;
  70.  
  71.     $maxImage = $currentPage * $imagesPerPage;
  72.     $minImage = ($currentPage-1) * $imagesPerPage;
  73.  
  74.  
  75.     if ($pageCount > 1)
  76.     {
  77.         $page_link= get_permalink();
  78.         $page_link_perma= true;
  79.         if ( strpos($page_link, '?')!==false )
  80.             $page_link_perma= false;
  81.  
  82.         $gplist= '<div class="gallery_pages_list">'.__('Pages').'&nbsp; ';
  83.         for ( $j=1; $j<= $pageCount; $j++)
  84.         {
  85.             if ( $j==$currentPage )
  86.                 $gplist .= '<strong class="current_gallery_page_num"> '.$j.' </strong>&nbsp;';
  87.             else
  88.                 $gplist .= '<a href="'.$page_link. ( ($page_link_perma?'?':'&amp;') ). 'galleryPage='.$j.'">'.$j.'</a>&nbsp; ';
  89.         }
  90.  
  91.         $gplist .= '</div>';
  92.     }
  93.     else
  94.         $gplist= '';
  95.  
  96.     $itemtag = tag_escape($options['itemtag']);
  97.     $captiontag = tag_escape($options['captiontag']);
  98.     $columns = intval($options['columns']);
  99.     $itemwidth = $options['columns'] > 0 ? floor(100/$options['columns']) : 100;
  100.     $float = is_rtl() ? 'right' : 'left';
  101.     $icontag = $options['icontag'];
  102.     $id = isset($options['id']) ? $options['id'] : '';
  103.     $size = $options['size'];
  104.  
  105.     $selector = "gallery-{$instance}";
  106.  
  107.     $gallery_style = $gallery_div = '';
  108.     if ( apply_filters( 'use_default_gallery_style', true ) )
  109.         $gallery_style = "
  110.         <style type='text/css'>
  111.             #{$selector} {
  112.                 margin: auto;
  113.             }
  114.             #{$selector} .gallery-item {
  115.                 float: {$float};
  116.                 margin-top: 10px;
  117.                 text-align: center;
  118.                 width: {$itemwidth}%;
  119.             }
  120.             #{$selector} img {
  121.                 border: 2px solid #cfcfcf;
  122.             }
  123.             #{$selector} .gallery-caption {
  124.                 margin-left: 0;
  125.             }
  126.         </style>
  127.         <!-- see gallery_shortcode() in wp-includes/media.php -->";
  128.     $size_class = sanitize_html_class( $size );
  129.     $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  130.     $output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
  131.  
  132.     $i = 0;
  133.     $k = 0;
  134.     foreach ( $attachments as $id => $attachment ) {
  135.         if ($k >= $minImage && $k < $maxImage) {
  136.             $link = isset($options['link']) && 'file' == $options['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
  137.  
  138.             $output .= "<{$itemtag} class='gallery-item'>";
  139.             $output .= "
  140.                 <{$icontag} class='gallery-icon'>
  141.                     $link
  142.                 </{$icontag}>";
  143.             if ( $captiontag && trim($attachment->post_excerpt) ) {
  144.                 $output .= "
  145.                     <{$captiontag} class='wp-caption-text gallery-caption'>
  146.                     " . wptexturize($attachment->post_excerpt) . "
  147.                     </{$captiontag}>";
  148.             }
  149.             $output .= "</{$itemtag}>";
  150.             if ( $columns > 0 && ++$i % $columns == 0 )
  151.                 $output .= '<br style="clear: both" />';
  152.         }
  153.         $k++;
  154.     }
  155.     $output .= "\n<br style='clear: both;' />$gplist\n</div>\n";
  156.  
  157.     return $output;
  158.  
  159.     // If we've got this far then we must have some attachments to play with!
  160.     //return 'Gallery Here - Page '. $currentPage. ' de '. $pageCount;
  161. }
  162.  
  163. add_shortcode('page-gallery', 'gallery_with_pagination');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement