title: ACF Repeater field pagination on homepage
body: <p>I'm using Advanced Custom Field plugin on my website to show products. I need to implement pagination to Repeater Field loop after showing 8 products. I found solution here -> <a href="http://jonathannicol.com/blog/2014/03/06/paginating-an-advanced-custom-fields-repeater/" rel="nofollow noreferrer">http://jonathannicol.com/blog/2014/03/06/paginating-an-advanced-custom-fields-repeater/</a> and this works great but not on the homepage. I was tested on mydomain.com/test and second page showing at mydomain.com/test/2 but on homepage site mydomain.com/2 showing 404 error.</p>
  
  <p>My code at template-home.php</p>
  
  <pre><code>&lt;div class="container"&gt;
  &lt;div class="row"&gt;
  
      &lt;?php
      $promocja = pll__('PROMOCJA!');
      $zobacz = pll__('Zobacz produkt'); ?&gt;
  
  
      &lt;div class="container productLoopContainer"&gt;
          &lt;div class="row"&gt;
  
          &lt;?php
          if( get_query_var('page') ) {
              $page = get_query_var( 'page' );
          } else {
              $page = 1;
          }
  
          $row              = 0;
          $cols_per_page    = 8; // How many images to display on each page
          $cols             = get_field( 'produkt' );
          $total            = count( $cols );
          $pages            = ceil( $total / $cols_per_page );
          $min              = ( ( $page * $cols_per_page ) - $cols_per_page ) + 1;
          $max              = ( $min + $cols_per_page ) - 1;
  
          if( have_rows('produkt') ) :
  
              while ( have_rows('produkt') ) : the_row();
  
                  $row++;
  
                  // Ignore this image if $row is lower than $min
                  if($row &lt; $min) { continue; }
  
                  // Stop loop completely if $row is higher than $max
                  if($row &gt; $max) { break; }    
  
                  $kategoria = get_sub_field('kategoria');
                  $urlProduktu = get_sub_field('adres_url');
                  $zdjecie = get_sub_field('zdjecie');
                  $nazwa = get_sub_field('nazwa_produktu'); ?&gt;
  
                      &lt;div class="col-sm-6 col-md-3"&gt;
                          &lt;div class="product clearfix product-hover" id="&lt;?php echo $kategoria; ?&gt;"&gt;
                              &lt;div class="left"&gt;
                                  &lt;div class="sale-badge"&gt;&lt;?php echo $promocja; ?&gt;&lt;/div&gt;
                                  &lt;div class="image"&gt;
                                      &lt;a href="&lt;?php echo $urlProduktu; ?&gt;"&gt;&lt;img src="&lt;?php echo $zdjecie; ?&gt;" class="zoom-image-effect wp-post-image" alt="&lt;?php echo $nazwa; ?&gt;"&gt;&lt;/a&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="right"&gt;
                                  &lt;div class="name"&gt;&lt;a href="&lt;?php echo $urlProduktu; ?&gt;"&gt;&lt;?php echo $nazwa; ?&gt;&lt;/a&gt;&lt;/div&gt;
                                  &lt;div class="typo-actions clearfix"&gt;
                                      &lt;div class="addtocart"&gt;
                                          &lt;a href="&lt;?php echo $urlProduktu; ?&gt;" class="button cart-links add_to_cart_button ajax_add_to_cart product_type_variable"&gt;&lt;?php echo $zobacz; ?&gt;&lt;/a&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
  
              &lt;?php endwhile;
  
              // Pagination
                echo paginate_links( array(
                  'base' =&gt; get_permalink() . '%#%' . '/',
                  'format' =&gt; '?page=%#%',
                  'current' =&gt; $page,
                  'total' =&gt; $pages
                ) ); ?&gt;
  
          &lt;?php else: 
              // no rows found
          endif; ?&gt;
  
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>Have you an idea how to fix this issue? I was trying with adding rewrite_endpoint in functions.php but without success.</p>
  
  <p>Thanks for your help!</p>
  
  <p>UPDATE:
  I added code to functions.php and a little bit change my template code and now there is no 404. Second page showing at mydomain.com/products/2 which is great but there is no products - instead of showing products there is only word "ARCHIVES".</p>
  
  <p>My template:</p>
  
  <pre><code>&lt;div class="container"&gt;
  &lt;div class="row"&gt;
  
      &lt;?php
      $promocja = pll__('PROMOCJA!');
      $zobacz = pll__('Zobacz produkt'); ?&gt;
  
  
      &lt;div class="container productLoopContainer"&gt;
          &lt;div class="row"&gt;
  
          &lt;?php
          // Get query var
                      global $wp_rewrite;
                      if ($wp_rewrite-&gt;using_permalinks()) {
                              if( !empty( get_query_var('products'))) {
                                      $page = get_query_var('products');
                              } else {
                                      $page = 1;
                              }
                      } else {
                              if( !empty( $_GET['products'] )) {
                                      $page = $_GET['products'];
                              } else {
                                      $page = 1;
                              }
                      }
  
          $row              = 0;
          $cols_per_page    = 8; // How many images to display on each page
          $cols             = get_field( 'produkt' );
          $total            = count( $cols );
          $pages            = ceil( $total / $cols_per_page );
          $min              = ( ( $page * $cols_per_page ) - $cols_per_page ) + 1;
          $max              = ( $min + $cols_per_page ) - 1;
  
          if( have_rows('produkt') ) :
  
              while ( have_rows('produkt') ) : the_row();
  
                  $row++;
  
                  // Ignore this image if $row is lower than $min
                  if($row &lt; $min) { continue; }
  
                  // Stop loop completely if $row is higher than $max
                  if($row &gt; $max) { break; }    
  
                  $kategoria = get_sub_field('kategoria');
                  $urlProduktu = get_sub_field('adres_url');
                  $zdjecie = get_sub_field('zdjecie');
                  $nazwa = get_sub_field('nazwa_produktu'); ?&gt;
  
                      &lt;div class="col-sm-6 col-md-3"&gt;
                          &lt;div class="product clearfix product-hover" id="&lt;?php echo $kategoria; ?&gt;"&gt;
                              &lt;div class="left"&gt;
                                  &lt;div class="sale-badge"&gt;&lt;?php echo $promocja; ?&gt;&lt;/div&gt;
                                  &lt;div class="image"&gt;
                                      &lt;a href="&lt;?php echo $urlProduktu; ?&gt;"&gt;&lt;img src="&lt;?php echo $zdjecie; ?&gt;" class="zoom-image-effect wp-post-image" alt="&lt;?php echo $nazwa; ?&gt;"&gt;&lt;/a&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="right"&gt;
                                  &lt;div class="name"&gt;&lt;a href="&lt;?php echo $urlProduktu; ?&gt;"&gt;&lt;?php echo $nazwa; ?&gt;&lt;/a&gt;&lt;/div&gt;
                                  &lt;div class="typo-actions clearfix"&gt;
                                      &lt;div class="addtocart"&gt;
                                          &lt;a href="&lt;?php echo $urlProduktu; ?&gt;" class="button cart-links add_to_cart_button ajax_add_to_cart product_type_variable"&gt;&lt;?php echo $zobacz; ?&gt;&lt;/a&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
  
              &lt;?php endwhile;
  
              // Pagination
                 if ($pages &gt; 1) {
                              repeater_pagination($page, $pages, 3);
                      } 
                      ?&gt;
  
          &lt;?php else: 
              // no rows found
          endif; ?&gt;
  
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>and functions.php: </p>
  
  <pre><code>function repeater_pagination($paged = 1, $pages = '', $range = 2) {
  $showitems = ($range * 2) + 1;
  if(1 != $pages) {
      echo "&lt;ul class='pager'&gt;";
      // Arrows left
      if ($paged &gt; 1) echo "&lt;li class='previous'&gt;&lt;a href='".get_repeater_pagenum_link($paged - 1)."'&gt;&lt;i class='icon-arrow-left'&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;";
      // Numbers
      for ($i=1; $i &lt;= $pages; $i++) {
              if (1 != $pages &amp;&amp;( !($i &gt;= $paged+$range+1 || $i &lt;= $paged-$range-1) || $pages &lt;= $showitems )) {
                      echo ($paged == $i)? "&lt;li class='active'&gt;&lt;a href='".get_repeater_pagenum_link($i)."'&gt;".$i."&lt;/a&gt;&lt;/li&gt;":"&lt;li&gt;&lt;a href='".get_repeater_pagenum_link($i)."'&gt;".$i."&lt;/a&gt;&lt;/li&gt;";
              }
      }
      // Arrows right
      if ($paged &lt; $pages) echo "&lt;li class='next'&gt;&lt;a href='".get_repeater_pagenum_link($paged + 1)."'&gt;&lt;i class='icon-arrow-right'&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;";
      echo "&lt;/ul&gt;\n";
    }
  }
  
  function get_repeater_pagenum_link($pagenum = 1) {
      global $wp_rewrite;
      $pagenum = (int) $pagenum;
      $pageslug = 'products';
      $request = remove_query_arg('products');
  
  $home_root = parse_url(home_url());
  $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
  $home_root = preg_quote( trailingslashit( $home_root ), '|' );
  $request = preg_replace('|^'. $home_root . '|', '', $request);
  $request = preg_replace('|^/+|', '', $request);
  
  // Normal Permalinks
  if ( !$wp_rewrite-&gt;using_permalinks() || is_admin() ) {
      $base = trailingslashit( get_bloginfo( 'url' ) );
      if ( $pagenum &gt; 1 ) {
              $result = add_query_arg('products', $pagenum, $base . `$request);`
      } else {
              $result = $base . $request;
      }
  // Pretty Permalinks   
  } else {
      $qs_regex = '|\?.*?$|';
      preg_match( $qs_regex, $request, $qs_match );
      if ( !empty( $qs_match[0] ) ) {
              $query_string = $qs_match[0];
              $request = preg_replace( $qs_regex, '', $request );
      } else {
              $query_string = '';
      }
      $request = preg_replace( "|$pageslug/\d+/?$|", '', $request);
      $request = preg_replace( '|^index\.php|', '', $request);
      $request = ltrim($request, '/');
      $base = trailingslashit( get_bloginfo( 'url' ) );
  
      if ( $wp_rewrite-&gt;using_index_permalinks() &amp;&amp; ( $pagenum &gt; 1 || '' != $request ) )
              $base .= 'index.php/';
  
      if ( $pagenum &gt; 1 ) {
              $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $pageslug . "/" . $pagenum, 'paged' );
      }
  
      $result = $base . $request . $query_string;
  }
  $result = apply_filters('get_repeater_pagenum_link', $result);
  return $result;
  }
  
  /* Products pagination parameter */
  function add_products_query_var(){
      global $wp;
      $wp-&gt;add_query_var('products');
  }
  /* Use EP_PERMALINK | EP_PAGES for pages and posts both */
  function products_rewrite_endpoint(){
    add_rewrite_endpoint('products', EP_ALL);
  }
  add_filter('init', 'add_products_query_var');
  add_filter('init', 'products_rewrite_endpoint');
  </code></pre>