我的分页不太有效。只有 4 个事件,分页设置为每页 3 个。当我加载第二页时,它会自行再次加载第一个事件,而不是最后一个帖子。
我的代码如下。我正在使用 kirby 内容管理器,但我认为这些东西在各个网站上非常统一。我很确定它不起作用,因为我的事件循环内部的循环,但我不知道如何纠正它。
<div id="main-container">
<div class="content">
<div class="container">
<div class="row">
<div class="col-md-12 content-block">
<ul class="events-list">
<?php $events = $page->children()->paginate(3);
$i = -1;
foreach($events as $event) {
$i++; ?>
<li class="event-list-item">
<?php $calendar = page('events')->children()->nth($i)->calendar()->yaml();
foreach($calendar as $calendarEntry) { ?>
<span class="event-date">
<span class="date">
<?php
$eventday=strtotime($calendarEntry['_begin_date']);
echo date('j',$eventday);
?>
</span>
<span class="month">
<?php
$eventmonth=strtotime($calendarEntry['_begin_date']);
echo date('M',$eventmonth);
?>
</span>
<span class="year">
<?php
$eventmonth=strtotime($calendarEntry['_begin_date']);
echo date('Y',$eventmonth);
?>
</span>
</span>
<div class="event-list-cont">
<span class="meta-data">Starts:
<?php $eventday=strtotime($calendarEntry['_begin_date']);
echo date('l',$eventday); ?>,
<?php $eventstarttime=strtotime($calendarEntry['_begin_time']);
echo date('g:i A',$eventstarttime); $eventday=strtotime($calendarEntry['_end_date']);
if ($eventday){ ?>
| Finishes: <?php echo date('l',$eventday); ?>,
<?php } ?>
<?php $eventendtime=strtotime($calendarEntry['_end_time']);
if($eventendtime) {
echo date('g:i A',$eventendtime);
} ?></span>
<h4 class="post-title"><a href="<?php echo page('events')->children()->nth($i)->url(); ?>"><?php echo $calendarEntry['eventname'] ?></a></h4>
<p>
<?php echo page('events')->children()->nth($i)->eventwriteup()->excerpt(250); ?> <a href="<?php echo page('events')->children()->nth($i)->url(); ?>">read more →</a>
</p>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
<!-- Page Pagination -->
<?php
$list = $page->children()->paginate(3);
$pagination = $list->pagination();
?>
<nav>
<ul class="pagination pagination-lg">
<?php if($pagination->hasPrevPage()): ?>
<li>
<a href="<?php echo $pagination->prevPageURL() ?>">←</a></li>
<?php else: ?>
<li><span>←</span></li>
<?php endif ?>
<?php foreach($pagination->range(10) as $r): ?>
<li><a<?php if($pagination->page() == $r) echo ' class="active"' ?> href="<?php echo $pagination->pageURL($r) ?>"><?php echo $r ?></a></li>
<?php endforeach ?>
<?php if($pagination->hasNextPage()): ?>
<li class="last"><a href="<?php echo $pagination->nextPageURL() ?>">→</a></li>
<?php else: ?>
<li class="last"><span>→</span></li>
<?php endif ?>
</ul>
</nav> ....
Wall,我很乐意在这里提供帮助,但需要更多上下文进入您的代码。你的循环中似乎有一些重复。不奇怪为什么你在循环中使用$i变量。从外观上看,您可以摆脱更简单的事情:
<div id="main-container">
<div class="content">
<div class="container">
<div class="row">
<div class="col-md-12 content-block">
<ul class="events-list">
<?php $events = $page->children()->paginate(3);
foreach($events as $event): ?>
<li class="event-list-item">
<?php $calendar = $event->calendar()->yaml();
foreach($calendar as $calendarEntry) { ?>
<span class="event-date">
<span class="date">
<?php
$eventday=strtotime($calendarEntry['_begin_date']);
echo date('j',$eventday);
?>
</span>
<span class="month">
<?php
$eventmonth=strtotime($calendarEntry['_begin_date']);
echo date('M',$eventmonth);
?>
</span>
<span class="year">
<?php
$eventmonth=strtotime($calendarEntry['_begin_date']);
echo date('Y',$eventmonth);
?>
</span>
</span>
<div class="event-list-cont">
<span class="meta-data">Starts:
<?php $eventday=strtotime($calendarEntry['_begin_date']);
echo date('l',$eventday); ?>,
<?php $eventstarttime=strtotime($calendarEntry['_begin_time']);
echo date('g:i A',$eventstarttime); $eventday=strtotime($calendarEntry['_end_date']);
if ($eventday){ ?>
| Finishes: <?php echo date('l',$eventday); ?>,
<?php } ?>
<?php $eventendtime=strtotime($calendarEntry['_end_time']);
if($eventendtime) {
echo date('g:i A',$eventendtime);
} ?></span>
<h4 class="post-title"><a href="<?php echo $event->url(); ?>"><?php echo $calendarEntry['eventname'] ?></a></h4>
<p>
<?php echo $event->eventwriteup()->excerpt(250); ?> <a href="<?php echo $event->url(); ?>">read more →</a>
</p>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
<!-- Page Pagination -->
<?php
$list = $page->children()->paginate(3);
$pagination = $list->pagination();
?>
<nav>
<ul class="pagination pagination-lg">
<?php if($pagination->hasPrevPage()): ?>
<li>
<a href="<?php echo $pagination->prevPageURL() ?>">←</a></li>
<?php else: ?>
<li><span>←</span></li>
<?php endif ?>
<?php foreach($pagination->range(10) as $r): ?>
<li><a<?php if($pagination->page() == $r) echo ' class="active"' ?> href="<?php echo $pagination->pageURL($r) ?>"><?php echo $r ?></a></li>
<?php endforeach ?>
<?php if($pagination->hasNextPage()): ?>
<li class="last"><a href="<?php echo $pagination->nextPageURL() ?>">→</a></li>
<?php else: ?>
<li class="last"><span>→</span></li>
<?php endif ?>
</ul>
</nav> ....
有关分页的更多信息,请查看这篇文章: 分页