Skip to content

Instantly share code, notes, and snippets.

@kisabelle
Last active September 19, 2023 05:12
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save kisabelle/2bffbc7435746fe07bcc to your computer and use it in GitHub Desktop.
Save kisabelle/2bffbc7435746fe07bcc to your computer and use it in GitHub Desktop.
ACF Repeater Field Pagination
<?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 = 10; // How many images to display on each page
$images = get_field( 'image_gallery' );
$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( 'image_gallery' ) ) : ?>
<?php while( have_rows( 'image_gallery' ) ): 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 $img_obj = get_sub_field( 'image' ); ?>
<a href="<?php echo $img_obj['sizes']['large']; ?>">
<img src="<?php echo $img_obj['sizes']['thumbnail']; ?>" alt="">
</a>
<?php endwhile;
// Pagination
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?page=%#%',
'current' => $page,
'total' => $pages
) );
?>
<?php else: ?>
No images found
<?php endif; ?>
@zalog
Copy link

zalog commented Mar 20, 2018

We can also use get_row_index() - docs.

@craftgit
Copy link

craftgit commented Dec 4, 2020

Doesn't work with WordPress version 5.5.3 :(

@kisabelle
Copy link
Author

Doesn't work with WordPress version 5.5.3 :(

Do you know which part isn't working? Is it the $page var?

@craftgit
Copy link

craftgit commented Dec 5, 2020

Found a solution.

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

and

'base' => get_permalink() . 'page/%#%' . '/', 'format' => '?paged=%#%',

@raju-jeelaga
Copy link

@craftgit

Thanks,
That's work for me :)

@wbq3510
Copy link

wbq3510 commented Nov 29, 2022

Found a solution.

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

and

'base' => get_permalink() . 'page/%#%' . '/', 'format' => '?paged=%#%',

Work ! Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment