Reference

$pages

The $pages class refers to a collection of pages. The pages in this collection can have the same or different parents, they can actually exist as subfolders in the content folder or be virtual pages created from a database, an Excel sheet, any API or any other source.

How to get a $pages object

The $pages variable is available by default in templates and snippets and refers to a collection of all first level pages. In addition to that, you can define a $pages object in various ways:

Children of a page

$pages = $page->children();

All pages of the site

$pages = $site->index();

A custom combination of pages

// find specific pages in the site
$pages = $site->find('blog', 'projects', 'contact');

// find specific subpages
$pages = $page->find('article-a', 'article-b');

Examples

With the $pages object in place, we can start using the methods of this class to do many different things:

Filter the collection by any criterium

$articles = $pages->filterBy('date', '>', time());

Get only listed pages

$articles = $pages->listed()

Get all files of all the pages in the collection

$files = $pages->files();

Get the first/last page

$firstPage = $pages->first();
$lastPage = $pages->last();

Sort pages

$sorted = $pages->sortBy(function ($page) {
  return $page->date()->toDate();
}, 'desc');