Created
July 31, 2020 05:41
-
-
Save wplit/d69281d3a14aa21df89486691d5197e3 to your computer and use it in GitHub Desktop.
acf pagination video subfields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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