Generating a trail of breadcrumbs became much simpler with Hugo v0.109.0 and later. Use the .Ancestors
method on .Page
.
{{- with .Ancestors.Reverse }}
<nav>
{{- $p := . | append $ }}
{{- range $k, $_ := $p }}
{{- if $k }}
<span class="breadcrumb-separator">»</span>
{{- end }}
{{- $ariaCurrent := "true" }}
{{- if eq $k (sub (len $p) 1) }}
{{- $ariaCurrent = "page" }}
{{- end }}
<a aria-current="{{ $ariaCurrent }}" href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{- end }}
</nav>
{{- end }}