Advertisement
Twansparant

ACF repeater field pagination

Feb 5th, 2014
3,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.25 KB | None | 0 0
  1. /* Template Loop */
  2. <div class="albums grid">
  3.     <?php
  4.     // Get query var
  5.     global $wp_rewrite;
  6.     if ($wp_rewrite->using_permalinks()) {
  7.         if( !empty( get_query_var('albums'))) {
  8.             $page = get_query_var('albums');
  9.         } else {
  10.             $page = 1;
  11.         }
  12.     } else {
  13.         if( !empty( $_GET['albums'] )) {
  14.             $page = $_GET['albums'];
  15.         } else {
  16.             $page = 1;
  17.         }
  18.     }
  19.     // Variables
  20.     $i      = 0;
  21.     $albumspp   = 9;
  22.     $albums     = get_field('photo_album');
  23.     $total      = count($albums);
  24.     $pages      = ceil($total / $albumspp);
  25.     $min        = (($page * $albumspp) - $albumspp) + 1;
  26.     $max        = ($min + $albumspp) - 1;
  27.     // Loop
  28.     if( have_rows('photo_album')) { ?>
  29.         <div class="row">
  30.             <?php while(have_rows('photo_album')): the_row(); $i++;
  31.                 /* Ignore this row, if $i is lower than $min */
  32.                 if($i < $min) {continue; }
  33.                 /* Stop loop completely, if $i is higher than $max */
  34.                 if($i > $max) {break; }            
  35.                 /* Close row after 3 albums and start new one */
  36.                 if($c == 4): $c = 0; ?>
  37.                     </div>
  38.                     <div class="row">
  39.                 <?php endif; ?>
  40.                 <article <?php post_class('photo-album col-xs-3'); ?>>
  41.                     <?php
  42.                     $album_title    = get_sub_field('photo_album_title');
  43.                     $album_thumb    = get_sub_field('photo_album_thumb');
  44.                     $album_gallery  = get_sub_field('photo_album_gallery');
  45.                     ?>
  46.                     <ul class="thumbnails">
  47.                         <li class="cover">
  48.                             <?php if ($album_thumb) {
  49.                                 $imgalt = $album_thumb['alt'];
  50.                                 $imgsrcsm = $album_thumb['sizes']['thumbnail'];
  51.                                 $imgsrcmd = $album_thumb['sizes']['medium'];
  52.                                 $imgsrclg = $album_thumb['sizes']['large'];
  53.                                 ?>
  54.                                 <div class="entry-image">
  55.                                     <a rel="lightbox[<?php echo $album_title; ?>]" href="<?php echo $imgsrclg; ?>" title="<?php echo $album_title; ?>">
  56.                                         <img src="<?php echo $imgsrcsm; ?>" alt="<?php echo $imgalt; ?>" />
  57.                                     </a>
  58.                                 </div>
  59.                             <?php } ?>
  60.                             <div class="entry-title">
  61.                                 <a rel="lightbox[<?php echo $album_title; ?>]" href="<?php echo $album_thumb['sizes']['large']; ?>" title="<?php echo $album_title; ?>">
  62.                                     <h5><?php echo $album_title; ?><i class="icon-arrow-right"></i></h5>
  63.                                 </a>   
  64.                             </div>
  65.                         </li>
  66.                         <!-- Hide the rest of the thumbnails -->
  67.                         <?php if ($album_gallery) {
  68.                             foreach ($album_gallery as $photo){ ?>
  69.                                 <li class="hide">
  70.                                     <a rel="lightbox[<?php echo $album_title; ?>]" href="<?php echo $photo['sizes']['large']; ?>" title="<?php echo $album_title; ?>">
  71.                                         <img src="<?php echo $photo['sizes']['thumbnail']; ?>" alt="<?php echo $photo['alt']; ?>" />
  72.                                     </a>
  73.                                 </li>
  74.                             <?php }
  75.                         } ?>   
  76.                     </ul>  
  77.                 </article>
  78.             <?php $c++; // column count
  79.         endwhile; ?>
  80.        
  81.     </div> 
  82.     <?php } else { ?>
  83.         <p><?php _e('No photo albums found','mytextdomain'); ?></p>
  84.     <?php } ?>
  85.    
  86.     <!-- Pagination -->
  87.     <?php if ($pages > 1) {
  88.         repeater_pagination($page, $pages, 3);
  89.     } ?>
  90. </div>
  91.  
  92. <?php
  93. /* ==========================================================================
  94.    ACF Repeater Field Pagination (in functions.php)
  95.    ========================================================================== */
  96. /**
  97.  * Custom bootstrap pagination for custom ACF repeater field array
  98.  */
  99. function repeater_pagination($paged = 1, $pages = '', $range = 2) {
  100.     $showitems = ($range * 2) + 1;
  101.     if(1 != $pages) {
  102.         echo "<ul class='pager'>";
  103.         // Arrows left
  104.         if ($paged > 1) echo "<li class='previous'><a href='".get_repeater_pagenum_link($paged - 1)."'><i class='icon-arrow-left'></i></a></li>";
  105.         // Numbers
  106.         for ($i=1; $i <= $pages; $i++) {
  107.             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
  108.                 echo ($paged == $i)? "<li class='active'><a href='".get_repeater_pagenum_link($i)."'>".$i."</a></li>":"<li><a href='".get_repeater_pagenum_link($i)."'>".$i."</a></li>";
  109.             }
  110.         }
  111.         // Arrows right
  112.         if ($paged < $pages) echo "<li class='next'><a href='".get_repeater_pagenum_link($paged + 1)."'><i class='icon-arrow-right'></i></a></li>";
  113.         echo "</ul>\n";
  114.     }
  115. }
  116.  
  117. /**
  118.  * Custom get_pagenum_link() function for custom paged links
  119.  */
  120. function get_repeater_pagenum_link($pagenum = 1) {
  121.     global $wp_rewrite;
  122.     $pagenum = (int) $pagenum;
  123.     $pageslug = 'albums';
  124.     $request = remove_query_arg('albums');
  125.    
  126.     $home_root = parse_url(home_url());
  127.     $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
  128.     $home_root = preg_quote( trailingslashit( $home_root ), '|' );
  129.     $request = preg_replace('|^'. $home_root . '|', '', $request);
  130.     $request = preg_replace('|^/+|', '', $request);
  131.    
  132.     // Normal Permalinks
  133.     if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
  134.         $base = trailingslashit( get_bloginfo( 'url' ) );
  135.         if ( $pagenum > 1 ) {
  136.             $result = add_query_arg('albums', $pagenum, $base . $request);
  137.         } else {
  138.             $result = $base . $request;
  139.         }
  140.     // Pretty Permalinks   
  141.     } else {
  142.         $qs_regex = '|\?.*?$|';
  143.         preg_match( $qs_regex, $request, $qs_match );
  144.         if ( !empty( $qs_match[0] ) ) {
  145.             $query_string = $qs_match[0];
  146.             $request = preg_replace( $qs_regex, '', $request );
  147.         } else {
  148.             $query_string = '';
  149.         }
  150.         $request = preg_replace( "|$pageslug/\d+/?$|", '', $request);
  151.         $request = preg_replace( '|^index\.php|', '', $request);
  152.         $request = ltrim($request, '/');
  153.         $base = trailingslashit( get_bloginfo( 'url' ) );
  154.        
  155.         if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
  156.             $base .= 'index.php/';
  157.  
  158.         if ( $pagenum > 1 ) {
  159.             $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $pageslug . "/" . $pagenum, 'paged' );
  160.         }
  161.        
  162.         $result = $base . $request . $query_string;
  163.     }
  164.     $result = apply_filters('get_repeater_pagenum_link', $result);
  165.     return $result;
  166. }
  167.  
  168. /* ==========================================================================
  169.    Rewrite Rules
  170.    ========================================================================== */
  171.  
  172. /* Photo albums pagination parameter */
  173. function add_albums_query_var(){
  174.     global $wp;
  175.     $wp->add_query_var('albums');
  176. }
  177. /* Use EP_PERMALINK | EP_PAGES for pages and posts both */
  178. function albums_rewrite_endpoint(){
  179.   add_rewrite_endpoint('albums', EP_PERMALINK | EP_PAGES);
  180. }
  181. add_filter('init', 'add_albums_query_var');
  182. add_filter('init', 'albums_rewrite_endpoint');
  183. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement