HUGO

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
gohugoio 加星
  • 关于
    • 本节内容
    • 简介
    • 特性
    • 隐私
    • 安全
    • 许可
  • 安装
    • 本节内容
    • macOS
    • Linux
    • Windows
    • BSD
  • 入门
    • 本节内容
    • 快速开始
    • 基本用法
    • 目录结构
    • 配置
    • 配置标记
    • 配置构建
    • 术语表
    • 外部学习资源
  • 快速参考
    • 本节内容
    • 表情符号
    • 函数
    • 方法
    • 页面集合
  • 内容管理
    • 本节内容
    • 组织
    • 页面包
    • 内容格式
    • Front matter
    • 构建选项
    • 页面资源
    • 图像处理
    • Shortcodes
    • 相关内容
    • 章节
    • 内容类型
    • 原型
    • 分类
    • 摘要
    • 链接和交叉引用
    • URL 管理
    • 菜单
    • 评论
    • 多语言
    • Markdown 属性
    • 语法高亮
    • 图表
    • 数学
    • 数据源
    • 内容适配器
  • 模板
    • 本节内容
    • 简介
    • 模板类型
    • 查找顺序
    • 基础模板
    • 首页模板
    • 单页模板
    • 章节模板
    • 分类模板
    • 术语模板
    • 部分模板
    • 内容视图模板
    • Shortcode 模板
    • 站点地图模板
    • RSS 模板
    • 404 模板
    • robots.txt 模板
    • 菜单
    • 分页
    • 嵌入式模板
    • 自定义输出格式
  • 函数
    • 本节内容
    • cast
    • collections
    • compare
    • crypto
    • css
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hash
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • 方法
    • 本节内容
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pager
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • 渲染钩子
    • 本节内容
    • 简介
    • 块引用
    • 代码块
    • 标题
    • 图像
    • 链接
    • 直通
    • 表格
  • Shortcodes
    • 本节内容
    • 评论
    • 详细信息
    • 图例
    • Gist
    • 高亮
    • Instagram
    • Param
    • QR
    • Ref
    • Relref
    • Vimeo
    • X
    • YouTube
  • Hugo 模块
    • 本节内容
    • 配置 Hugo 模块
    • 使用 Hugo 模块
    • 主题组件
  • Hugo Pipes
    • 本节内容
    • 简介
    • 将 Sass 转译为 CSS
    • PostCSS
    • 后处理
    • JavaScript 构建
    • 资源压缩
    • 连接资源
    • 指纹识别和 SRI 哈希
    • 从字符串创建资源
    • 从模板创建资源
  • CLI
  • 故障排除
    • 本节内容
    • 审计
    • 日志
    • 检查
    • 弃用
    • 性能
    • 常见问题解答
  • 开发者工具
    • 本节内容
    • 编辑器插件
    • 前端
    • 搜索
    • 迁移
    • 其他项目
  • 托管和部署
    • 本节内容
    • Hugo Deploy
    • 使用 Rclone 部署
    • 使用 Rsync 部署
    • 在 21YunBox 上托管
    • 在 AWS Amplify 上托管
    • 在 Azure 静态 Web 应用上托管
    • 在 Cloudflare Pages 上托管
    • 在 Firebase 上托管
    • 在 GitHub Pages 上托管
    • 在 GitLab Pages 上托管
    • 在 KeyCDN 上托管
    • 在 Netlify 上托管
    • 在 Render 上托管
  • 贡献
    • 本节内容
    • 开发
    • 文档
    • 主题
  • 维护
方法 页面方法

祖先

返回一个页面对象集合,每个对象对应给定页面的一个祖先章节。

语法

PAGE.Ancestors

返回

page.Pages

章节是指顶层内容目录,或任何包含 _index.md 文件的内容目录。

使用此内容结构

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

另请参阅

  • CurrentSection
  • FirstSection
  • InSection
  • IsAncestor
  • IsDescendant
  • Parent
  • 章节

本节内容

  • Aliases
  • AllTranslations
  • AlternativeOutputFormats
  • 祖先
  • BundleType
  • CodeOwners
  • Content
  • ContentWithoutSummary
  • CurrentSection
  • Data
  • Date
  • Description
  • Draft
  • Eq
  • ExpiryDate
  • File
  • FirstSection
  • Fragments
  • FuzzyWordCount
  • GetPage
  • GetTerms
  • GitInfo
  • HasMenuCurrent
  • HasShortcode
  • HeadingsFiltered
  • InSection
  • IsAncestor
  • IsDescendant
  • IsHome
  • IsMenuCurrent
  • IsNode
  • IsPage
  • IsSection
  • IsTranslated
  • Keywords
  • Kind
  • Language
  • Lastmod
  • Layout
  • Len
  • LinkTitle
  • Next
  • NextInSection
  • OutputFormats
  • Page
  • PAGE.Store
  • Pages
  • Paginate
  • Paginator
  • Param
  • Params
  • Parent
  • Path
  • Permalink
  • Plain
  • PlainWords
  • Prev
  • PrevInSection
  • PublishDate
  • RawContent
  • ReadingTime
  • Ref
  • RegularPages
  • RegularPagesRecursive
  • RelPermalink
  • RelRef
  • Render
  • RenderShortcodes
  • RenderString
  • Resources
  • Scratch
  • Section
  • 章节
  • Site
  • Sitemap
  • Sites
  • Slug
  • Summary
  • TableOfContents
  • Title
  • TranslationKey
  • Translations
  • Truncated
  • Type
  • Weight
  • WordCount
最后更新时间:2024 年 11 月 18 日:删除旧的 new-in 徽章 (d50ed3422)
改进此页面
由 Hugo 作者 提供
Hugo Logo
  • 提交问题
  • 获取帮助
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo 赞助商

贵公司?
 

Hugo 徽标版权归 Steve Francia 2013–2025 所有。

Hugo 地鼠基于 Renée French 的原创作品。

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
  • 关于
  • 安装
  • 入门
  • 快速参考
  • 内容管理
  • 模板
  • 函数
  • 方法
  • 渲染钩子
  • Shortcodes
  • Hugo 模块
  • Hugo Pipes
  • CLI
  • 故障排除
  • 开发者工具
  • 托管和部署
  • 贡献
  • 维护