[英]How to paginate a foreach loop?
I have this foreach loop wich shows the supporters located in the post_meta from a custom post type. 我有这个foreach循环,其中显示了来自自定义帖子类型的支持者位于post_meta中 。 What I want to do is add pagination to the foreach loop.
我想做的是将分页添加到foreach循环中。 I have already found a way to decide how many supporters are shown by slicing the array, but now I am at a loss.
我已经找到了一种方法,可以通过分割阵列来确定显示多少支持者,但是现在我很茫然。 And have no idea how to proceed.
而且不知道如何进行。
Function to get the supporters array 获取支持者数组的函数
function getSupporters($petitieID){
$support = get_post_meta(get_the_ID(), 'supporters', true);
if (!empty($support)){
return $support;
}}
Function to show the individual supporters in the array 用于显示阵列中各个支持者的功能
function showSupporters($petitieID){
$supporters = getSupporters($petitieID);
if (!empty($supporters)){
foreach (array_slice($supporters, 0, 2) as $supporter){
$supporterID = $supporter->post_author;
the_author_meta('first_name', $supporterID);
}
}else {
echo 'no votes';
}
}
You could determine which page is currently shown in a GET variable in your address 您可以确定当前在地址中的GET变量中显示哪个页面
.../supporters.php?page=1
Then you could set the offset of your array_slice
function accordingly 然后您可以相应地设置
array_slice
函数的偏移量
$nItemsPerPage = 2;
$page = isset($_GET['page'])?$_GET['page']:1;
array_slice($supporters, $nItemsPerPage*($page-1), $nItemsPerPage)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.