Skip to content

Instantly share code, notes, and snippets.

@wplit
Created July 31, 2020 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wplit/d69281d3a14aa21df89486691d5197e3 to your computer and use it in GitHub Desktop.
Save wplit/d69281d3a14aa21df89486691d5197e3 to your computer and use it in GitHub Desktop.
acf pagination video subfields
<?php
/*
* Paginatation on Advanced Custom Fields Repeater
*/
if( get_query_var('page') ) {
$page = get_query_var( 'page' );
} else {
$page = 1;
}
// Variables
$row = 0;
$items_per_page = 3;
$items = get_field( 'videos' );
$total = count( $items );
$pages = ceil( $total / $items_per_page );
$min = ( ( $page * $items_per_page ) - $items_per_page ) + 1;
$max = ( $min + $items_per_page ) - 1;
// ACF Loop
if( have_rows( 'videos' ) ) {
echo '<ul class="videos">';
while( have_rows( 'videos' ) ): 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; }
preg_match( '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', get_sub_field( 'video_url' ), $match );
$video_id = $match[1];
printf( '<li class="video"><h2 class="video-title"><a href="%s">%s</a></h2><div class="iframe-wrapper"><iframe width="560" height="315" src="https://www.youtube.com/embed/%s" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div></li>', get_sub_field( 'video_url' ), get_sub_field( 'video_title' ), $video_id );
endwhile;
echo '</ul>';
// Pagination
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?page=%#%',
'current' => $page,
'total' => $pages
) );
} else {
echo 'No videos found';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment