Pagination with Gallery field – Advanced Custom Fields
If you work a lot with WordPress, I’m sure you are familiar with the plugin Advanced Custom Fields (ACF). If not, you better go check it out, as this is absolutely one of the best plugins out there. With the PRO version you can use repeater, gallery and flexible content field. Woohoo!
Repeater field and gallery field, are really awesome. There is one thing tho – there is no pagination for that. But don’t worry, as there is a solution for that!
Pagination for gallery field
The pagination for gallery and repeater are a bit different, so I will start with the solution for gallery field. First thing you need to know – even tho pagination will not be generated by some magic function in WP – if you add “number” to the end of the url, you can get current pagination number with “get_query_var(‘paged’)“. Simple as that. And for pagination you can use WP function paginate_links.
Here is full code:
//Setup pagination variables $gallery = get_field('gallery'); // Get our gallery $images = array(); // Set images array for current page $items_per_page = 12; // How many items we should display on each page $total_items = count($gallery); // How many items we have in total $total_pages = ceil($total_items / $items_per_page); // How many pages we have in total //Get current page if ( get_query_var( 'paged' ) ) { $current_page = get_query_var( 'paged' ); }elseif ( get_query_var( 'page' ) ) { //this is just in case some odd rewrite, but paged should work instead of page here $current_page = get_query_var( 'page' ); }else{ $current_page = 1; } $starting_point = (($current_page-1)*$items_per_page); // Get starting point for current page // Get elements for current page if($gallery){ $images = array_slice($gallery,$starting_point,$items_per_page); } if(!empty($images)){ //your gallery loop here } // And our pagination $big = 999999999; // need an unlikely integer echo paginate_links(array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => $current_page, 'total' => $total_pages, 'before_page_number' => '<span class="screen-reader-text">'.__('Page ','textdomain').' </span>' ));
In case you want just “load more” button, instead of “paginate_links” you could do something like that:
if($total_pages>1 && $current_page<$total_pages){ echo '<a href="'.get_permalink().'page/'.($current_page+1).'/">'.__('Load more','textdomain').'</a>'; }
For the next/prev buttons only:
// Set previous page if($total_pages>1 && $current_page<=$total_pages && $current_page!=1){ echo '<a href="'.get_permalink().(($current_page-1)!=1 ? 'page/'.($current_page-1).'/' : '').'">'.__('Previous','textdomain').'</a>'; } // Set next page if($total_pages>1 && $current_page<$total_pages && $current_page!=$total_pages){ echo '<a href="'.get_permalink().'page/'.($current_page+1).'/">'.__('Next','textdomain').'</a>'; }
Bonus code – Pagination with internal filter/categories
Update 24.04.2016
If you want to divide gallery into categories or something, and you want to have custom subpages for each category, you will have to add some rewrite rules. Everything started after comment from Manhattan and some emails we exchange to resolve this. Long story short I present you a gist where you can find details about this solution.
This helped me so much. Code is clean, legible and – with your write-up – very easy to implement. THANK YOU! Spent a day on what would otherwise have taken me weeks :)
Thank you very much, I used the code and it works perfectly
Thank you very much!
Very very helpful!
Very helpful, thanx (y)
Thank you for this. Wondering if I can get some help to do the pagination without the “/page/” in the url, ie: [example.com/gallery/1] instead of [example.com/gallery/page/1]
Looks like I was able to do it (I hope it’s okay to do it this way) by using:
‘base’ => get_permalink() . ‘%#%’ . ‘/’,
instead of:
‘base’ => str_replace( $big, ‘%#%’, esc_url( get_pagenum_link( $big ) ) ),
And I found this solution from using a similar method for paginating the repeater field type, here:
https://jonathannicol.com/blog/2014/03/06/paginating-an-advanced-custom-fields-repeater/
nice it work.. first it wasn’t working. later i am able to make it work :)
What do you need to add for the gallery loop? //your gallery loop here
I added the code to output a gallery that I got from acf https://www.advancedcustomfields.com/resources/gallery/
It just outputs all of the images no matter what I define in your code. Am I missing something?
@sam. you can do some like this
if( $images ): ?>
<a href="” class=”modal” rel=”lightbox[gallery]” title=””>
<img src="” alt=”” />
So what should be img src? I see that $images is an array.
” alt=”” /> show the first image.
I’m beginner.
Thank your for work great.
Would you give me sample code, in case I want just “load more” button with ajax loading paging and append result to DOM?
I am looking your help.
thanks,
Thankss¡¡
Great share, thank you Eliza!
I’m having a hard time to implement this solution with different rewrite rules.
Regex drives me crazy!
Using a CPT ‘Gallery’ this is the url structure I’d like to have:
cpt-gallery/post-name/
cpt-gallery/post-name/page/x
cpt-gallery/post-name/collection/collection-name
cpt-gallery/post-name/collection/collection-name/page/x
(where collection-name is the category_name var)
Any idea how to implement this?
Thanks!
After several hours of search and debugging here’s what I came up with.
function gallery_cat_rewrite() {
add_rewrite_rule(
‘gallery/([^/]*)/([^/]*)/page/([0-9])/?$’,
‘index.php?post_type=gallery&name=$matches[1]&category_name=$matches[2]&paged=$matches[3]’,
‘top’
);
add_rewrite_rule(
‘gallery/([^/]*)/([^/]*)/?$’,
‘index.php?post_type=gallery&name=$matches[1]&category_name=$matches[2]’,
‘top’
);
add_rewrite_rule(
‘gallery/([^/]*)/page/([0-9])/?$’,
‘index.php?post_type=gallery&name=$matches[1]&paged=$matches[2]’,
‘top’
);
}
Needed to add the following to make pagination work with my cpt.
add_filter( ‘redirect_canonical’, ‘custom_disable_redirect_canonical’ );
function custom_disable_redirect_canonical( $redirect_url ) {
if ( is_singular(‘gallery’) ) $redirect_url = false;
return $redirect_url;
}
Thanks!
” Everything started after comment from Manhattan and some emails we exchange to resolve this. ”
-> Everything started after f…… mail harassment from Manhattan and some hard work from me to solve its f…… code.
;p
Lol. Close enough ;o)
No worries, happy to help
Thank you so much for this post. I got a page with ACF galleries that I mix (in an array). And it works great. But when I want to add some categories for my galleries, by using this kind of code : http://www.wp-spread.com/amusons-nous-avec-lurl-rewriting-et-le-passage-de-parametres-vers-une-page-wordpress/ it works great to, but I can’t make it work with your code (with the pagination). mysite.com/photos/category1 works mysite.com/photos/category2 works mysite.com/photos/1/ works mysite.com/photos/2/ works but mysite.com/photos/category2/1/ (or 2) doesn’t work. Is it possible to mix pagination with custom query url or not ? Sorry for my bad french english, sorry for bother you, and another time, thanks… Read more »
Manhattan, I don’t see why it wouldn’t work. Do you get 404 or simply it’s stuck on first page? If it’s stuck, then try changing this:
to this
or even better change it to:
and let me know if it’s working :o)
That still doesn’t work.
I think that I need to adapt my url rewriting code to insert pagination but I don’t know how :
global $wp_rewrite;
add_rewrite_tag(‘%catphotos%’,'([^&]+)’);
$wp_rewrite->add_rule(‘photos/([^/]+)’,’index.php?pagename=photos&catphotos=$matches[1]’,’top’);
$wp_rewrite->flush_rules();
you try this add in your function.php file
add_filter( ‘redirect_canonical’, ‘custom_disable_redirect_canonical’ );
function custom_disable_redirect_canonical( $redirect_url ) {
if ( is_paged() && is_singular() ) $redirect_url = false;
return $redirect_url;
}
I can confirm, this filter work. Thanks
Works brilliantly. Thank you so much.
Very Nice and Helpful Materiel Thanks :)