| 1 |
--- |
| 2 |
title: Ancestors |
| 3 |
description: Returns a collection of Page objects, one for each ancestor section of the given page. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: page.Pages |
| 9 |
signatures: [PAGE.Ancestors] |
| 10 |
--- |
| 11 |
|
| 12 |
With this content structure: |
| 13 |
|
| 14 |
```tree |
| 15 |
content/ |
| 16 |
├── auctions/ |
| 17 |
│ ├── 2023-11/ |
| 18 |
│ │ ├── _index.md <-- front matter: weight = 202311 |
| 19 |
│ │ ├── auction-1.md |
| 20 |
│ │ └── auction-2.md |
| 21 |
│ ├── 2023-12/ |
| 22 |
│ │ ├── _index.md <-- front matter: weight = 202312 |
| 23 |
│ │ ├── auction-3.md |
| 24 |
│ │ └── auction-4.md |
| 25 |
│ ├── _index.md <-- front matter: weight = 30 |
| 26 |
│ ├── bidding.md |
| 27 |
│ └── payment.md |
| 28 |
├── books/ |
| 29 |
│ ├── _index.md <-- front matter: weight = 10 |
| 30 |
│ ├── book-1.md |
| 31 |
│ └── book-2.md |
| 32 |
├── films/ |
| 33 |
│ ├── _index.md <-- front matter: weight = 20 |
| 34 |
│ ├── film-1.md |
| 35 |
│ └── film-2.md |
| 36 |
└── _index.md |
| 37 |
``` |
| 38 |
|
| 39 |
And this template: |
| 40 |
|
| 41 |
```go-html-template |
| 42 |
{{ range .Ancestors }} |
| 43 |
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a> |
| 44 |
{{ end }} |
| 45 |
``` |
| 46 |
|
| 47 |
On the November 2023 auctions page, Hugo renders: |
| 48 |
|
| 49 |
```html |
| 50 |
<a href="/auctions/2023-11/">Auctions in November 2023</a> |
| 51 |
<a href="/auctions/">Auctions</a> |
| 52 |
<a href="/">Home</a> |
| 53 |
``` |
| 54 |
|
| 55 |
In the example above, notice that Hugo orders the ancestors from closest to furthest. This makes breadcrumb navigation simple: |
| 56 |
|
| 57 |
```go-html-template |
| 58 |
<nav aria-label="breadcrumb" class="breadcrumb"> |
| 59 |
<ol> |
| 60 |
{{ range .Ancestors.Reverse }} |
| 61 |
<li> |
| 62 |
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a> |
| 63 |
</li> |
| 64 |
{{ end }} |
| 65 |
<li class="active"> |
| 66 |
<a aria-current="page" href="{{ .RelPermalink }}">{{ .LinkTitle }}</a> |
| 67 |
</li> |
| 68 |
</ol> |
| 69 |
</nav> |
| 70 |
``` |
| 71 |
|
| 72 |
With some CSS, the code above renders something like this, where each breadcrumb links to its page: |
| 73 |
|
| 74 |
```text |
| 75 |
Home > Auctions > Auctions in November 2023 > Auction 1 |
| 76 |
``` |