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.
wp-config
to see if you get any error messages?var_dump($page);
to see if it's actually updating the offset properly?