antknight Posted March 5, 2013 Share Posted March 5, 2013 Hi Guys, I have spent a fair amount of time trying to figure this out but I need some help. I have the following structure: Home --Section One ----News ------News Item --Section Two ----News ------News Item If I am on the home page I want to display a list of all the latest news items in the sidebar: if($page->path === '/') { $news = $pages->find("template=news-item, limit=5, sort=-date"); echo "<ul>"; foreach ($news as $newsitem) { echo "<li><a href='{$newsitem->url}'>{$newsitem->rootParent->title} - {$newsitem->title}</a></li>"; echo "<span>{$newsitem->date}</span>"; } echo "</ul>"; } If I am on any other page I want to display a list of all the latest news items FOR THAT SECTION in the sidebar, I can't figure it out Link to comment Share on other sites More sharing options...
FvG Posted March 5, 2013 Share Posted March 5, 2013 Maybe something like parent=/section_two/? Link to comment Share on other sites More sharing options...
teppo Posted March 5, 2013 Share Posted March 5, 2013 $news = $page->rootParent->find("template=news-item, limit=5, sort=-date"); echo "<ul>"; foreach ($news as $newsitem) { echo "<li><a href='{$newsitem->url}'>{$newsitem->rootParent->title} - {$newsitem->title}</a></li>"; echo "<span>{$newsitem->date}</span>"; } echo "</ul>"; That should be enough. There's no need for "if($page->path === '/')" either, since $page->rootParent always points to parent page closest to root except while viewing root page itself (in which case it points to current page.) Exactly what you need for a task like this 2 Link to comment Share on other sites More sharing options...
interrobang Posted March 5, 2013 Share Posted March 5, 2013 $news = $page->rootParent->find("template=news-item, sort=-date, limit=5 "); echo "<ul>"; foreach ($news as $newsitem) { echo "<li><a href='{$newsitem->url}'>{$newsitem->rootParent->title} - {$newsitem->title}</a></li>"; echo "<span>{$newsitem->date}</span>"; } echo "</ul>"; Edit: I am too slow... But I think you have to swap sort and limit if you want the latest 5 news. Link to comment Share on other sites More sharing options...
antknight Posted March 5, 2013 Author Share Posted March 5, 2013 Thanks Teppo it works! That is pretty slick that I can remove the if statement! I also wasn't aware that the order of selectors mattered, limit then sort or sort then limit, does it make a difference? Link to comment Share on other sites More sharing options...
teppo Posted March 5, 2013 Share Posted March 5, 2013 @antknight: they don't matter - in this case. You should take a look at this post by nik where he explains pretty comprehensively how this stuff works 2 Link to comment Share on other sites More sharing options...
antknight Posted March 5, 2013 Author Share Posted March 5, 2013 Great stuff. Excellent reference. Link to comment Share on other sites More sharing options...
ryan Posted March 6, 2013 Share Posted March 6, 2013 I also wasn't aware that the order of selectors mattered, limit then sort or sort then limit, does it make a difference? It doesn't matter. Though if you had multiple "sort=something" statements, then it would consider the order of the sort statements. Meaning, if you had "sort=last_name, sort=first_name" then the results would first be sorted by last_name and then by first_name. Whereas "sort=first_name, sort=last_name" would do the opposite. 1 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