Generating Breadcrumbs for Hugo

Sweet merciful go gods this is really handy. I had a convoluted mess until I found .Ancestors!

If anyone needs a schema.org JSON-LD version of this:

{{ if not .IsHome }}
      <script type='application/ld+json'>
      {
        "@context": "https://schema.org",
        "@type": "BreadcrumbList",
        "itemListElement":
        [
        {{- $i := 1 }}
        {{- range .Ancestors.Reverse }}
          {
            "@type": "ListItem",
            "position": {{ $i }},
            "name": "{{ .Title | safeJS }}",
            "url": {{ .Permalink }},
            "item": {{ .Permalink }}
          },
          {{- $i = add $i 1 -}}
        {{ end }}
          {
            "@type": "ListItem",
            "position": {{ $i }},
            "name": "{{ .Title | safeJS }}",
            "url": {{ .Permalink }},
            "item": {{ .Permalink }}
          }
        ]
      }
      </script>
{{ end }}
3 Likes