Пагинация в ACF Repeater
Advanced Custom Fields (ACF) — это ‘must have’ плагин для создания любого сайта на WordPress. Он позволяет с лёгкостью создавать, управлять и выводить произвольные поля в любом месте вашего шаблона. Один из мощных инструментов ACF — дополнение Repeater Field, которое позволяет создавать повторяющиеся группы полей. Однако, если у вас много записей в Repeater, страница может стать слишком длинной и неудобной для пользователей. В этой статье я покажу, как добавить пагинацию для Repeater Field в ACF, чтобы сделать вашу галерею изображений более удобной.
Когда на странице используется Repeater Field с большим количеством записей, это может привести к чрезмерному увеличению длины страницы. Пагинация позволяет разбить контент на более управляемые части, улучшая пользовательский опыт.
Для этого примера мы будем добавлять пагинацию к полю image_gallery, содержащему дочернее поле image, в котором хранятся объекты изображений. Наш Repeater будет выводиться на странице с URL /gallery.
Ну и собственно сам код:
<?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; ?>
Источник: http://jonathannicol.com/blog/2014/03/06/paginating-an-advanced-custom-fields-repeater/
Is there a way to reverse the order of the rows, so that the most recently added repeater item would appear on the first page?
Check please this article: https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/
Выдает такую ошибку: Warning: count(): Parameter must be an array or an object that implements Countable in и возвращает не корректное значение