分页工作但不太正确。在第二页重复第一篇文章

时间:2016-03-18 03:54:09

标签: php pagination kirby

我的分页不太合适。每页只有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&nbsp;more&nbsp;→</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() ?>">&larr;</a></li>
                <?php else: ?>
                <li><span>&larr;</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() ?>">&rarr;</a></li>
              <?php else: ?>
              <li class="last"><span>&rarr;</span></li>
              <?php endif ?>
            </ul>
          </nav> ....

1 个答案:

答案 0 :(得分:0)

墙, 我很乐意在这里提供帮助,但需要在代码中加入更多上下文。你的循环似乎有些重复。不是为什么你也在你的循环中使用$ 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&nbsp;more&nbsp;→</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() ?>">&larr;</a></li>
                <?php else: ?>
                <li><span>&larr;</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() ?>">&rarr;</a></li>
              <?php else: ?>
              <li class="last"><span>&rarr;</span></li>
              <?php endif ?>
            </ul>
          </nav> ....

有关分页的详细信息,请查看此帖子:Pagination