Getting the URL of the last page

As the title says I need to get the url of the last page. I used $pagination->lastPageUrl() from the docs but it’s not working for me. Does anyone know what I’m doing wrong here

Controller — projects.php

<?php

return function($site, $pages, $page) {
    $projects = page('projects')->children()->visible();
    $first = $projects->first();
    return compact('projects', 'first');
};

Snippet in Template — projects.php

<nav class="pagination">
    <a class="pagination-item" href="<?= $projects->lastPageUrl() ?>" rel="prev">
        Previous
    </a>

    <a class="pagination-item" href="<?= $projects->nth(1)->url() ?>" rel="next">
        Next
    </a>
</nav>

Is that the full extent of your controller? Doesn’t look like your actually paging anything. Refer to this page in the cookbook.

Yes, I guess I don’t actually need pagination but to just get the URL of the last page in $projects.

<?= $projects->last()->url() ?> solved my problem

I would suggest using the pagination. With the way you have done it, you will probably end up dead links on the first and last page since your not checking that a next page actually exists:

<?php if($articles->pagination()->hasNextPage()): ?>
  <a class="next" href="<?= $articles->pagination()->nextPageURL() ?>">&lsaquo; older posts</a>
  <?php endif ?>

Ok, I think you’re right. I basically want to create a looping image slider. I set ‘projects’ as the home page in config c::set('home', 'projects');

Page structure:

projects
___ project-1
___ project-2
___ project-3
___ project-4

The ‘projects’ home page should display the first child content (project-1) and have a navigation that links to the second page (project-2) and the last page (project-4). Which would be set in the projects.php template.

In the project.php template the navigation should link to the previous and next pages, in the case of the first page (project-1) the previous page would be the last page (project-4) and in the case of the last page the next page would be the first page (project-1).

Is pagination the right way to approach this?

Im confused by the looping image slider part. For that you just need a loop, and Im assuming its javascript powered - most slider scripts have built in controls to allow navigation to between slides without leaving the page. I usually use Tiny Slider.

If your talking about just clicking through pages, I don’t think you can use next() from the projects page, because there is no next page at that level since all your projects are children, but you could link the project page to the first project and use next() and prev() from there (in your inividual project template)

<?php if($next = $page->next()): ?>
<a href="<?= $next->url() ?>">next page</a>
<?php endif ?>

But again, you would need some logic in there to link the last page back to the first.

<?php if($next = $page->next()): ?>
<a href="<?= $next->url() ?>">next page</a>
<?php else: ?>
<!-- FIRST PROJECT LINK -->
<?php endif ?>