Collections
Sorting
You can control the order in which your collection items are sorted. When iterating over your collection in a @foreach
loop, the items will be returned in the order you specify.
Default Sort
By default, collection items will be sorted by their filenames in ascending order. This can be a handy way to order items by date, for instance, by prepending the date to each filename:
12017-01-01-my-first-post.md22017-02-14-my-second-post.md32017-03-15-my-third-post.md4...
Sorting by a Variable
You can also sort your collection by the values of variables defined in the YAML front matter of each collection item. Add a sort
key to the collection's array in config.php
, and specify the name of the field to sort by:
config.php
1<?php2 3return [4 'collections' => [5 'posts' => [6 'sort' => 'sort_order',7 ],8 ],9];
To sort by multiple variables (for a hierarchical sort), specify an array of variables:
config.php
1<?php2 3return [4 'collections' => [5 'posts' => [6 'sort' => ['featured', 'date'],7 ],8 ],9];
To sort in descending order, prepend a -
to the variable name:
config.php
1<?php2 3return [4 'collections' => [5 'posts' => [6 'sort' => ['featured', '-date'],7 ],8 ],9];