hugo/docs/content/en/methods/pages/GroupByPublishDate.md
Bjørn Erik Pedersen 5fd1e74903
Merge commit '9b0050e9aabe4be65c78ccf292a348f309d50ccd' as 'docs'
```
git subtree add --prefix=docs/ https://github.com/gohugoio/hugoDocs.git master --squash
```

Closes #11925
2024-01-27 10:48:57 +01:00

69 lines
2 KiB
Markdown

---
title: GroupByPublishDate
description: Returns the given page collection grouped by publish date in descending order.
categories: []
keywords: []
action:
related:
- methods/pages/GroupByDate
- methods/pages/GroupByExpiryDate
- methods/pages/GroupByLastMod
- methods/pages/GroupByParamDate
returnType: page.PagesGroup
signatures: ['PAGES.GroupByPublishDate LAYOUT [SORT]']
---
When grouping by publish date, the value is determined by your [site configuration], defaulting to the `publishDate` field in front matter.
The [layout string] has the same format as the layout string for the [`time.Format`] function. The resulting group key is [localized] for language and region.
[`time.Format`]: /functions/time/format/
[layout string]: #layout-string
[localized]: /getting-started/glossary/#localization
[site configuration]: /getting-started/configuration/#configure-dates
{{% include "methods/pages/_common/group-sort-order.md" %}}
To group content by year and month:
```go-html-template
{{ range .Pages.GroupByPublishDate "January 2006" }}
<p>{{ .Key }}</p>
<ul>
{{ range .Pages }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
{{ end }}
```
To sort the groups in ascending order:
```go-html-template
{{ range .Pages.GroupByPublishDate "January 2006" "asc" }}
<p>{{ .Key }}</p>
<ul>
{{ range .Pages }}
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
{{ end }}
</ul>
{{ end }}
```
The pages within each group will also be sorted by publish date, either ascending or descending depending on your 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:
```go-html-template
{{ range .Pages.GroupByPublishDate "January 2006" }}
<p>{{ .Key }}</p>
<ul>
{{ range .Pages.ByTitle }}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{ end }}
</ul>
{{ end }}
```
## Layout string
{{% include "functions/_common/time-layout-string.md" %}}