antknight Posted March 5, 2013 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
teppo Posted March 5, 2013 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
interrobang Posted March 5, 2013 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.
antknight Posted March 5, 2013 Author 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?
teppo Posted March 5, 2013 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
ryan Posted March 6, 2013 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
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