HUGO
Menu
GitHub 87548 stars Mastodon

Ancestors

返回 Page 對象的集合,每個代表給定頁面的一個祖先欄目。

Syntax

PAGE.Ancestors

Returns

page.Pages

使用此內容結構:

content/
├── auctions/
│   ├── 2023-11/
│   │   ├── _index.md     <-- front matter: weight = 202311
│   │   ├── auction-1.md
│   │   └── auction-2.md
│   ├── 2023-12/
│   │   ├── _index.md     <-- front matter: weight = 202312
│   │   ├── auction-3.md
│   │   └── auction-4.md
│   ├── _index.md         <-- front matter: weight = 30
│   ├── bidding.md
│   └── payment.md
├── books/
│   ├── _index.md         <-- front matter: weight = 10
│   ├── book-1.md
│   └── book-2.md
├── films/
│   ├── _index.md         <-- front matter: weight = 20
│   ├── film-1.md
│   └── film-2.md
└── _index.md

以及此模板:

{{ range .Ancestors }}
  <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}

在 2023 年 11 月拍賣頁面上,Hugo 渲染:

<a href="/auctions/2023-11/">Auctions in November 2023</a>
<a href="/auctions/">Auctions</a>
<a href="/">Home</a>

在上面的示例中,注意 Hugo 從最近到最遠排序祖先。這使得面包屑導航變得簡單:

<nav aria-label="breadcrumb" class="breadcrumb">
  <ol>
    {{ range .Ancestors.Reverse }}
      <li>
        <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
      </li>
    {{ end }}
    <li class="active">
      <a aria-current="page" href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
    </li>
  </ol>
</nav>

使用一些 CSS,上面的代碼渲染如下,每個面包屑鏈接到其頁面:

Home > Auctions > Auctions in November 2023 > Auction 1

Last updated: January 1, 0001
Improve this page