StephenOC Posted February 6, 2016 Share Posted February 6, 2016 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 More sharing options...
LostKobrakai Posted February 6, 2016 Share Posted February 6, 2016 <?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> 1 Link to comment Share on other sites More sharing options...
StephenOC Posted February 6, 2016 Author Share Posted February 6, 2016 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 More sharing options...
LostKobrakai Posted February 6, 2016 Share Posted February 6, 2016 $portfolioPage->asmSelect->getNext($current); $portfolioPage->asmSelect->getPrev($current); $portfolioPage->asmSelect->first(); $portfolioPage->asmSelect->last(); 2 Link to comment Share on other sites More sharing options...
StephenOC Posted February 6, 2016 Author Share Posted February 6, 2016 Sorry LostKobrakai ... I don't understand. Can you clarify further for me please? Thanks. Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 6, 2016 Share Posted February 6, 2016 $portfolioPage is the page object for the portfolio page, asmSelect needs to be changed with the fieldname of your asmselect field. Then replace all prev/next/last/first calls in my last example with the new calls. 1 Link to comment Share on other sites More sharing options...
StephenOC Posted February 6, 2016 Author Share Posted February 6, 2016 So to get $portfolioPage do I need to do what I done to start with? $portfolioPage = $pages->get('/portfolio/');? Link to comment Share on other sites More sharing options...
tpr Posted February 6, 2016 Share Posted February 6, 2016 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 1 Link to comment Share on other sites More sharing options...
BitPoet Posted February 6, 2016 Share Posted February 6, 2016 <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. 1 Link to comment Share on other sites More sharing options...
StephenOC Posted February 6, 2016 Author Share Posted February 6, 2016 Thanks guys. This works perfectly now. I have a lot to learn, but I am loving the way ProcessWire works. Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 6, 2016 Share Posted February 6, 2016 Just to clarify my code snippets in comparison to BitPoet's. $page->prev($portfolio_pages) === $portfolio_pages->getPrev($page) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now