Jump to content

Continuous Prev/Next Links


StephenOC
 Share

Recommended Posts

I am fairly new to PW and I am struggling to get my previous/next links to work once the end/beginning is reached. I have a list of portfolio items displayed in a specific order (ASM Selector) on my portfolio page. I'd like the previous/next links to follow the order of the portfolio items on the portfolio page. So in each portfolio item page I have the following code:

<div class="pagination">
     <?php
          $portfolio_pages = $pages->get('/portfolio/')->projects;
     ?>
     <div class="left-arrow">
          <a href="<?php echo $page->prev($portfolio_pages)->url; ?>" title="Previous Project"><?php echo file_get_contents('assets/static-images/left-arrow.svg'); ?></a>
     </div>
     <div class="all-projects">
          <a href="<?php echo $config->urls->root; ?>portfolio/" title="All Projects"><?php echo file_get_contents('assets/static-images/all-projects-icon.svg'); ?></a>
     </div>
     <div class="right-arrow">
          <a href="<?php echo $page->next($portfolio_pages)->url; ?>" title="Next Project"><?php echo file_get_contents('assets/static-images/right-arrow.svg'); ?></a>
     </div>
</div>

But when the beginning or end is reached there is no previous/next link url, it's just blank.

How can I get my links to link back to the end/start once the first/last item is reached?

Thanks.

Link to comment
Share on other sites


<?php $prev = $page->prev->id ? $page->prev : $page->siblings->last(); ?>

<div class="left-arrow">

<a href="<?php echo $prev->url; ?>" title="Previous Project">

<?php echo file_get_contents('assets/static-images/left-arrow.svg'); ?>

</a>

</div>

<?php $next = $page->next->id ? $page->next : $page->siblings->first(); ?>

<div class="left-arrow">

<a href="<?php echo $next->url; ?>" title="Next Project">

<?php echo file_get_contents('assets/static-images/right-arrow.svg'); ?>

</a>

</div>

  • Like 1
Link to comment
Share on other sites

Thanks LostKobrakai, however this is not in the order that my portfolio items appear on my portfolio page, this is the order they appear in the admin of ProcessWire.

On the portfolio page I have a 'projects' field which is of type 'page' and I use ASM Select to select the portfolio items I want to appear on the portfolio page and what order they appear in.

Is there a way to tie the previous/next links to the order that they appear on the portfolio page?

Thanks.

Link to comment
Share on other sites

It's not for ASMselect but something similar should work after adjustments:

// prev-next Pages are NullPages if current is first or last
$prevPage = $page->prev->id ? $page->prev : $page->siblings->last;
$nextPage = $page->next->id ? $page->next : $page->siblings->first;

// use $prevPage->url
  • Like 1
Link to comment
Share on other sites

<div class="pagination">
     <?php
          $portfolio_pages = $pages->get('/portfolio/')->projects;
          $prev = $page->prev($portfolio_pages)->id ? $page->prev($portfolio_pages) : $portfolio_pages->last();
          $next = $page->next($portfolio_pages)->id ? $page->next($portfolio_pages) : $portfolio_pages->first();

     ?>
     <div class="left-arrow">
          <a href="<?php echo $prev->url; ?>" title="Previous Project"><?php echo file_get_contents('assets/static-images/left-arrow.svg'); ?></a>
     </div>
     <div class="all-projects">
          <a href="<?php echo $config->urls->root; ?>portfolio/" title="All Projects"><?php echo file_get_contents('assets/static-images/all-projects-icon.svg'); ?></a>
     </div>
     <div class="right-arrow">
          <a href="<?php echo $next->url; ?>" title="Next Project"><?php echo file_get_contents('assets/static-images/right-arrow.svg'); ?></a>
     </div>
</div>

$portfolio_pages is a PageArray. $page->prev/next looks for the position of $page in the PageArray, then returns the preceding/following page. In case of the first/last item, where there is no such, it returns a NullPage object with an id of zero. So you can check for that id and get the last/first item in the PageArray instead.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...