Allow custom sorting for collections (#2723)
* Allow custom sorting for collections * Update docs with custom sort of collections * Refactoring
This commit is contained in:
parent
29c403b3f6
commit
6282bd9221
2 changed files with 12 additions and 13 deletions
|
@ -1,17 +1,11 @@
|
||||||
{% assign entries = site[include.collection] %}
|
{% assign entries = site[include.collection] %}
|
||||||
|
|
||||||
{% if include.sort_by == 'title' %}
|
{% if include.sort_by %}
|
||||||
{% if include.sort_order == 'reverse' %}
|
{% assign entries = entries | sort: include.sort_by %}
|
||||||
{% assign entries = entries | sort: 'title' | reverse %}
|
{% endif %}
|
||||||
{% else %}
|
|
||||||
{% assign entries = entries | sort: 'title' %}
|
{% if include.sort_order == 'reverse' %}
|
||||||
{% endif %}
|
{% assign entries = entries | reverse %}
|
||||||
{% elsif include.sort_by == 'date' %}
|
|
||||||
{% if include.sort_order == 'reverse' %}
|
|
||||||
{% assign entries = entries | sort: 'date' | reverse %}
|
|
||||||
{% else %}
|
|
||||||
{% assign entries = entries | sort: 'date' %}
|
|
||||||
{% endif %}
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{%- for post in entries -%}
|
{%- for post in entries -%}
|
||||||
|
|
|
@ -250,7 +250,7 @@ This layout displays all documents grouped by a specific collection. It accommod
|
||||||
collection: # collection name
|
collection: # collection name
|
||||||
entries_layout: # list (default), grid
|
entries_layout: # list (default), grid
|
||||||
show_excerpts: # true (default), false
|
show_excerpts: # true (default), false
|
||||||
sort_by: # date (default) title
|
sort_by: # date (default), title or any metadata key added to the collection's documents
|
||||||
sort_order: # forward (default), reverse
|
sort_order: # forward (default), reverse
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -264,6 +264,11 @@ collection: recipes
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to sort the collection by title add `sort_by: title`. If you want reverse sorting, add `sort_order: reverse`.
|
If you want to sort the collection by title add `sort_by: title`. If you want reverse sorting, add `sort_order: reverse`.
|
||||||
|
You can also use any metadata key that is present in the documents. For example, you can add `number: <any number>` to your documents and use `number` as the sort key:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
sort_by: number
|
||||||
|
```
|
||||||
|
|
||||||
### `layout: category`
|
### `layout: category`
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue