Tuesday, January 10, 2017

ACF Repeater Field Pagination

Hello Friends,

Few days ago, I found difficulties in pagination for ACF Repeater Custom Field. I have worked on custom post type before on pagination but this one is newer for me. I have googled lots of article but nothing matches my requirement then after 10 Hrs. of digging I found saver code (which saves my time). Haha.

Below is the code which might help you too. If it isn't solve your problem then send me your code so, I can help out.

In below code replace your parameter with matching ones.

<?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; ?>

A blogger

I am passionate blogger cum B.Tech. Computer engineering graduate. I love writing blog post. I spend my free time in writing blog post that will useful to everyone (including me). I have had some success making money blogging and want to help others do the same. I just figured that by creating a great and free resource a lot of links would follow – and they have. Some people ask me how they can repay me – which is not necessary - but for those wanting to show their appreciation, I just say linking to the article from their blog is the best compensation I could receive. Thanks for reading!

0 comments:

Post a Comment

Thanks for commenting. I will Reply you soon

 

Copyright @ 2013 PHP WORDPRESS MAGENTO .

Designed by Kcon Technosoft