Navigation / pagination loop

Hi,

I’m trying to achieve an infinite loop that allows a rotation of next / prev pages. Right now I have three pages, and when the user gets to the last page, I’d like him / her to have the option to return to the first page. Here’s my code:

<?php
$subpages   = $page->siblings($self = true)->paginate(3);
$pagination = $subpages->pagination();
?>
<nav class="nextprev cf" role="navigation">
      <hr>
      <?php if($prev = $page->prevVisible()): ?>
      <a class="prev" href="<?php echo $prev->url() ?>">&larr; <?php echo $prev->title()->html() ?></a>
       <?php else: ?>
          <a class="prev" href="<?php echo $pagination->lastpageUrl() ?>">&larr; <?php echo $pagination->lastPageUrl() ?></a>
      <?php endif ?>
      <?php if($next = $page->nextVisible()): ?>
      <a class="next" href="<?php echo $next->url() ?>"><?php echo $next->title()->html() ?> &rarr;</a>
      <?php else: ?>
      <a class="next" href="<?php echo $pagination->firstpageUrl() ?>"><?php echo $pagination->firstPageUrl() ?> &rarr;</a>
      <?php endif ?>
    </nav>

I’d also like to return the HTML value of the lastpageURL andfirstpageURL for the end user. I’m sure the solution here is something really simple, so appreciate any help you can provide!

This should work (without the pagination object, just calling the first and last pages of the collection):

<?php
$subpages   = $page->siblings()->paginate(3);
?>
<nav class="nextprev cf" role="navigation">
<hr>
<?php if($prev = $page->prevVisible()): ?>
      <a class="prev" href="<?php echo $prev->url() ?>">&larr; <?php echo $prev->title()->html() ?></a>
<?php else: ?>
     <a class="prev" href="<?php echo $subpages->last()->url() ?>">&larr; <?php echo $subpages->last()->url() ?></a>
 <?php endif ?>
<?php if($next = $page->nextVisible()): ?>
     <a class="next" href="<?php echo $next->url() ?>">
        <?php echo $next->title()->html() ?> &rarr;</a>
<?php else: ?>
      <a class="next" href="<?php echo $subpages->first()->url() ?>"><?php echo $subpages->first()->url() ?> &rarr;</a>
<?php endif ?>
</nav>

Awesome sauce, @texnixe! Thanks for the pointer- swapped the echo $subpages->first()->url() portion of the else loop with echo $subpages->first()->title()->html() to get the HTML value and this puppy works like a charm!

Thanks- does this need to be changed to a solution?

One more thing I just thought of… what if we knew that the number of pages wasn’t fixed? Suppose the administrator adds a page and we now have four pages?

How can we modify the paginate number, changing it from a fixed integer / number to a variable?

<?php $subpages = $page->siblings()->paginate(3); ?>

I have another section in my site that I’m layering navigation onto, but it will likely be a variable number of pages. How can we achieve this? I’m looking at the Kirby docs on pagination and I don’t see a way to paginate a variable number of pages. Hmm…

Why would you want to change the paginate number in the first place? Because the number you pass just means that you want to display 3 pages per page if you want to display all pages, you don’t have to use the pagination method at all.

Having said that, you can of course pass a variable, e.g.

<?php
$limit = 3;
$subpages = $page->siblings()->paginate($limit);
?>

Of course, $limitcould be anything, not just a fixed number, e.g.

$limit= $pages->count()/3;

Thanks- I just want to loop through a small number of pages, so $limit works well. In one instance I know my page collection will be fixed- just three pages in that- but when I reuse this for another page collection, I know that number will vary widely so this variable works great.

One more thing- I want to display a header image to represent the pagination images for each page, along with the title. I have a header.svg image under every page, so the name is the same regardless of page.

The idea is that both images and text are both represented for the user as they navigate from page to page. Here’s my code now:

    <?php
$limit= $pages->find('services')->children()->count();
$subpages   = $page->siblings()->paginate($limit);
$subpage = $page->sibling(self="true");
$image= $subpage->image('header.svg');
?><?php if($prev = $page->prevVisible()): ?>
          <a class="prev" href="<?php echo $prev->url() ?>">&larr; <img src="<?php echo $prev->$image->url();?><?php echo $prev->title()->html() ?></a>
    <?php else: ?>
         <a class="prev" href="<?php echo $subpages->last()->url() ?>">&larr; <img src="<?php echo $subpages->last()->$image->url();?><?php echo $subpages->last()->title()->html() ?></a>
     <?php endif ?>
    <?php if($next = $page->nextVisible()): ?>
         <a class="next" href="<?php echo $next->url() ?>"><img src="<?php echo $next->$image->url();?><?php echo $next->title()->html() ?> &rarr;</a>
    <?php else: ?>
          <a class="next" href="<?php echo $subpages->first()->url() ?>"><img src="<?php echo $subpages->first()->$image->url();?><?php echo $subpages->first()->title()->html() ?> &rarr;</a>
    <?php endif ?>

Unfortunately, I seem not to be able to call the images. Ideas? Thanks again in advance for saving the day!