AnotherAndrew Posted March 22, 2013 Share Posted March 22, 2013 My site structure is something like this: parent item 1 --sibling --sibling --sibling --sibling parent item 2 parent item 3 Within parent item 1, I have a menu that navigates to each sibling page like this: <nav> <ul id="pages"> <li><a class="previous" href="<?php echo $page->prev->path; ?>">prev</a></li> <li><a class="next" href="<?php echo $page->next->path; ?>">next</a></li> </ul> </nav> What I would like to do is change my li class when ever a user would get to the first and last sibling page. Does anyone know how I can do that? Link to comment Share on other sites More sharing options...
diogo Posted March 22, 2013 Share Posted March 22, 2013 This page's previous sibling page, or NullPage if it is the first sibling.† See also $page->prev($pageArray). edit: to test if a page is a NullPage, we can check if the "id" property exists on that object. If it doesn't have, it is indeed a NullPage. http://processwire.com/api/types/nullpage/ if($page->prev->id){ echo "this isn't the first page" } same thing happens with $page->next So: <nav> <ul id="pages"> <li><a class="<?=$page->prev->id ? 'previous' : 'first'?>" href="<?php echo $page->prev->path; ?>">prev</a></li> <li><a class="<?=$page->next->id ? 'next' : 'last'?>" href="<?php echo $page->next->path; ?>">next</a></li> </ul> </nav> ps: written in the browser and not tested Edit: reread the cheatsheet text, and it says "NullPage", and not nule or false. So we can't test it like I did above. What we have to do is to test if the page has id. I will rewrite the code above. Link to comment Share on other sites More sharing options...
AnotherAndrew Posted March 22, 2013 Author Share Posted March 22, 2013 Thanks Diogo. Your code doesn't return the first and last classes as is. Would it not be something like this <?php echo $page->prev ? 'first' : 'previous'?> Link to comment Share on other sites More sharing options...
diogo Posted March 22, 2013 Share Posted March 22, 2013 <?=$page->prev->id ? 'previous' : 'first'?> means: if there is a page before this (this isn't the first), the class is "previous", if there isn't, the class is "first". But I don't know what you want to do exactly. You have to adjust to your intention. You could also simply not print the link at all for the first page: if($page->prev->id) { echo "<li><a class='previous' href='{$page->prev->path}'>prev</a></li>"; } 1 Link to comment Share on other sites More sharing options...
AnotherAndrew Posted March 22, 2013 Author Share Posted March 22, 2013 Diogo. Thanks for pointing me in the right direction. I think I will try a little bit of both. Adding a class and canceling the link! Link to comment Share on other sites More sharing options...
Peter Knight Posted May 23, 2015 Share Posted May 23, 2015 I know this is an old thread but I needed a back and next link for a template and the solution here was great. I know it's not very process-wirey but I just hide the previous and next link if there is no previous and next page. <nav> <ul id="prev-next"> <li> <a class="<?=$page->prev->id ? 'previous' : 'no-before'?>" href="<?php echo $page->prev->path; ?>"> <strong><</strong> <?=$page->prev->title?> </a> </li> <li> <a class="<?=$page->next->id ? 'next' : 'no-after'?>" href="<?php echo $page->next->path; ?>"> <?=$page->next->title?> <strong>></strong> </a> </li> </ul> </nav> and then ul#prev-next { margin:0; } #prev-next li{ display:inline; list-style:none; } #prev-next a.no-after { display: none; } #prev-next a.no-before { display: none; } #prev-next a.previous { margin-right: 1rem; } 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