Jump to content

Return only pages in the current section?


antknight
 Share

Recommended Posts

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

$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 :)

  • Like 2
Link to comment
Share on other sites

$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

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. 

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...