Jump to content

Recommended Posts

Posted

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 }
  ?>
Posted

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
Posted

I think that's the right way indeed. But I'm surprised that siblings return also the page itself...

  • Like 1
Posted

Diogo, I guess it works this way as it's a very common need to find a position of current page or it's neighbour element in retured array. Just used it recently myself ;)

  • Like 1
Posted

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
Posted

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

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
×
×
  • Create New...