Jump to content

Detect first and last sibling page


AnotherAndrew
 Share

Recommended Posts

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

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

<?=$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>";
}
  • Like 1
Link to comment
Share on other sites

  • 2 years later...

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

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

×
×
  • Create New...