Jump to content

List all siblings exclude current page


onjegolders
 Share

Recommended Posts

Was just wondering about the best way to show a list of siblings (in this case other news articles) in the sidebar without also showing the current page article?

Currently I have this but not sure on how to "offset" the current page:

<?php $siblings = $page->siblings();
   foreach ($siblings as $sibling) { ?>
       <li><a href="<?php echo $sibling->url; ?>"><?php echo $sibling->title; ?></a></li>
<?php }
  ?>
Link to comment
Share on other sites

Think I may have sorted it with:

<?php $siblings = $page->siblings();
$siblings->remove($page);
foreach ($siblings as $sibling) { ?>
<li><a href="<?php echo $sibling->url; ?>"><?php echo $sibling->title; ?></a></li>
<?php }
?>

Would be interested in finding out if this is the right way of going about it or I just got lucky? ;)

Thanks.

  • Like 1
Link to comment
Share on other sites

makes sense, although in a purely semantic logic i would say a page can't be sibling of itself, and for me it would make more sense to write

$page->parent->children for that... even if longer.

  • Like 1
Link to comment
Share on other sites

makes sense, although in a purely semantic logic i would say a page can't be sibling of itself, and for me it would make more sense to write

$page->parent->children for that... even if longer.

Yeah I think sibling would imply that it excludes the current page but the result in the end is OK.

I would guess this sort of code is used quite a lot for articles, blogs, related entries etc...

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...