Debian Codegraph @

docs/content/en/methods/pages/GroupByDate.md

raw
1 ---
2 title: GroupByDate
3 description: Returns the given page collection grouped by date in descending order.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: page.PagesGroup
9 signatures: ['PAGES.GroupByDate LAYOUT [SORT]']
10 ---
11
12 ## Usage
13
14 When grouping by date, the value is determined by your [project configuration][], defaulting to the `date` field in front matter.
15
16 The [layout string](#layout-string) has the same format as the layout string for the [`time.Format`][] function. The resulting group key is [localized](g) for language and region.
17
18 {{% include "/_common/methods/pages/group-sort-order.md" %}}
19
20 To group content by year and month:
21
22 ```go-html-template
23 {{ range .Pages.GroupByDate "January 2006" }}
24 <p>{{ .Key }}</p>
25 <ul>
26 {{ range .Pages }}
27 <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
28 {{ end }}
29 </ul>
30 {{ end }}
31 ```
32
33 To sort the groups in ascending order:
34
35 ```go-html-template
36 {{ range .Pages.GroupByDate "January 2006" "asc" }}
37 <p>{{ .Key }}</p>
38 <ul>
39 {{ range .Pages }}
40 <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
41 {{ end }}
42 </ul>
43 {{ end }}
44 ```
45
46 The pages within each group will also be sorted by date, either ascending or descending depending on the grouping option. To sort the pages within each group, use one of the sorting methods. For example, to sort the pages within each group by title:
47
48 ```go-html-template
49 {{ range .Pages.GroupByDate "January 2006" }}
50 <p>{{ .Key }}</p>
51 <ul>
52 {{ range .Pages.ByTitle }}
53 <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
54 {{ end }}
55 </ul>
56 {{ end }}
57 ```
58
59 ## Layout string
60
61 {{% include "/_common/time-layout-string.md" %}}
62
63 [`time.Format`]: /functions/time/format/
64 [project configuration]: /configuration/front-matter/#dates