0

Since updating to php7.2 the pagination function has stopped working on a gallery built with ACF repeater. I suspect it's an issue with the count() function

I've found some articles relating to this but can't work out how to update my code to resolve the problem. If anyone could help, I'd be super grateful.

My Code below

<?php
/* 
 * Paginate Advanced Custom Field repeater
 */

if( get_query_var('page') ) {
  $page = get_query_var( 'page' );
} else {
  $page = 1;
}

// Variables
$row              = 0;
$images_per_page  = 15; // How many images to display on each page
$images           = get_sub_field( 'main_gallery_images' );
$total            = count( $images );
$pages            = ceil( $total / $images_per_page );
$min              = ( ( $page * $images_per_page ) - $images_per_page ) + 1;
$max              = ( $min + $images_per_page ) - 1;

// ACF Loop
if( have_rows( 'main_gallery_images' ) ) : ?>
    <div id="gallery" class="main">
  <?php while( have_rows( 'main_gallery_images' ) ): the_row();

    $row++;

    // Ignore this image if $row is lower than $min
    if($row < $min) { continue; }

    // Stop loop completely if $row is higher than $max
    if($row > $max) { break; } ?>                     

    <?php 
      $image = get_sub_field( 'image' ); 
      $caption = get_sub_field('caption');
    ?>
    <div class="galleryItem">
        <a href="javascript:void(0)" class="enlarge">
            <div class="image" style="background-image: url(<?php echo $image; ?>)">
            <span><?php echo $caption; ?><em>CLOSE X</em></span>
            </div>
        </a>
    </div>
  <?php endwhile; ?>
    </div>
<div class="galPag">
<?php // Pagination
  echo paginate_links( array(
    'base' => get_permalink() . '%#%' . '/#gallery',
    'format' => '?page=%#%',
    'current' => $page,
    'total' => $pages
  ) );
  ?>
</div>
<?php endif; ?>

Site is here: http://kalmkitchendev.wpengine.com/gallery/

What should happen is that the pagination links update the url and display the next set of images. What is happening is that the url gets updated but you just get the first set of images displayed again. The 'next' link doesn't update and is always pointing to page2.

If the first set of images are always shown, regardless which sub-page you’re on, it probably means that the “page” query var does not exist. The conditional at the very top of the code checks for its existence, and sets $page to 1 as a fallback, which is likely what’s happening but I can't work out how to fix it.

I've added the following to debug the issue.

global $wp_query; 
var_dump($wp_query->query_vars);

Which Prints out.

array(65) { ["pagename"]=> string(7) "gallery" ["do_not_redirect"]=> int(1) ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(7) "gallery" ["static"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["embed"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["post_name__in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["lazy_load_term_meta"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["post_type"]=> string(0) "" ["posts_per_page"]=> int(10) ["nopaging"]=> bool(false) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) ["order"]=> string(4) "DESC" } 

If anyone can help I'd be very happy.

  • Did you turn on debugging in wp-config to see if you get any error messages? – Jos Apr 26 at 12:55
  • Thanks Jos, Yes i added the following to config... define( 'WP_DEBUG', true ); define( 'WP_DEBUG_DISPLAY', true ); couldn't see any error messages though – SurreyCreative Apr 26 at 12:57
  • Can you var_dump($page); to see if it's actually updating the offset properly? – Alexander Holsgrove Apr 26 at 13:59
  • Thanks Alexander, Yes I've dumped out the variables I'll update the question with the output although if you take a look at the dev site it is testing on you will be able to see this kalmkitchendev.wpengine.com/gallery – SurreyCreative May 8 at 10:30
0

Turns out that there is just a small change to the way $page variable is set and the way the pagination links need to be written out.

the variable of page needs to be updated to paged and in the pagination array

page/ needs to be prefixed to %#%

Corrected example below.

Hope this helps someone with the same issue in the future.

if( get_query_var('paged') ) {
  $page = get_query_var( 'paged' );
} 

<?php // Pagination
  echo paginate_links( array(
    'base' => get_permalink() . 'page/%#%' . '/#gallery',
    'format' => '?page=%#%',
    'current' => $page,
    'total' => $pages
  ) );
  ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.