Hugo v0.109.0 “added a new .Ancestors method on Page that walks up the tree to the home page. With this, breadcrumbs templates can be greatly simplified”. Below is a simple example of breadcrumbs that I use on my site.
If your site is multilingual, define {{ i18n "home" }} in your i18n folder with the respective language equivalent for Homepage or whatever you fancy. If your site is not multilingual, replace {{ i18n "home" }} with an SVG Icon, text (Home etc.) or whatever word you fancy. Cheers!
I tuned this code snippet to make the breadcrumbs consistent with Schema.org structured data and rich Google results. Thanks to the .Ancestor method, the breadcrumb code is much simpler and more readable than before.
<ol class="" itemscope itemtype="https://schema.org/BreadcrumbList">
{{- /* declare a 'variable' to store the each link position */}}
{{- $data := newScratch }}
{{- range $index, $value := .Ancestors.Reverse }}
<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
{{- /* read the index from loop and add 'one', because it starts counting from zero */}}
{{- $data.Set "counter" $index }}
{{- $data.Add "counter" 1 }}
<a itemprop="item" href="{{.Permalink}}">
<span itemprop="name">{{.Title}}</span>
</a>
{{- /* pass the counter value into schema attribute */}}
<meta itemprop="position" content='{{ $data.Get "counter"}}' />
</li>
{{- end }}
<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
{{- /* add 'one' one more time for the last link position */}}
{{- $data.Add "counter" 1 }}
<a itemprop="item" href="{{ .Permalink }}">
<span itemprop="name">{{.Title}}</span>
</a>
{{- /* pass the counter value into schema attribute */}}
<meta itemprop="position" content='{{ $data.Get "counter"}}' />
</li>
</ol>