onjegolders Posted April 17, 2012 Share Posted April 17, 2012 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 More sharing options...
onjegolders Posted April 17, 2012 Author Share Posted April 17, 2012 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. 1 Link to comment Share on other sites More sharing options...
diogo Posted April 17, 2012 Share Posted April 17, 2012 I think that's the right way indeed. But I'm surprised that siblings return also the page itself... 1 Link to comment Share on other sites More sharing options...
slkwrm Posted April 17, 2012 Share Posted April 17, 2012 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 1 Link to comment Share on other sites More sharing options...
diogo Posted April 17, 2012 Share Posted April 17, 2012 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. 1 Link to comment Share on other sites More sharing options...
onjegolders Posted April 17, 2012 Author Share Posted April 17, 2012 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 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