我正试图反转我的中继器以及添加分页。
我能够成功完成两项任务,但不能将两者结合起来。
反转顺序的代码来自ACF:
https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/
添加分页的代码来自:
ACF Gallery Pagination in Worpdress
这是我的代码:
<?php
$repeater = get_field('summaries');
$order = array();
foreach( $repeater as $i => $row ) {
$order[ $i ] = $row;
}
array_multisort( $order, SORT_DESC, $repeater );
// Pagination
$items_per_page = 10;
$total_items = count($repeater);
$total_pages = ceil($total_items / $items_per_page);
if( get_query_var('paged') ){
$current_page = get_query_var('paged');
}
elseif ( get_query_var('page') ) {
$current_page = get_query_var('page');
}
else{
$current_page = 1;
}
$starting_point = (($current_page-1)*$items_per_page);
if($repeater){
$row = array_slice($repeater,$starting_point,$items_per_page);
}
if( $repeater ) : ?>
<?php foreach( $repeater as $i => $row ): ?>
<div class="container mt4 mb4 articles flexbox-v">
<strong>Summary: </strong>
<?= $row['summary']; ?>
</div>
<?php endforeach; ?>
<?php endif;
$big = 999999999;
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">'.$translated.' </span>'
));
?>
它仍然以相反的顺序输出中继器并增加了分页功能。但是,第二页项目只是第一页项目的重复。
示例。第1页:项目1-12,第2页:项目1-12
因此本质上我需要修复分页功能以使其正常工作,但我不确定从何处去。
谢谢!