I want to output my breadcrumbs using schema, but the @id
repeats the URL twice or sometimes more than that?! So if I visit the about
page, I see:
"@id":"http://localhost:1313/about/http://localhost:1313/about/",
When I use pagination, it repeats the URL even more:
"@id":"http://localhost:1313/blog/http://localhost:1313/blog//http://localhost:1313/blog/http://localhost:1313/blog//http://localhost:1313/blog/http://localhost:1313/blog/",
The code I am using: Taken from: https://gohugohq.com/partials/breadcrumb-partial-with-structured-data-in-hugo/
{{ $url := replace .Permalink ( printf "%s" .Site.BaseURL) "" }}
{{ $.Scratch.Add "path" .Site.BaseURL }}
{{ $.Scratch.Add "breadcrumb" (slice (dict "url" .Site.BaseURL "name" "home" "position" 1 )) }}
{{ range $index, $element := split $url "/" }}
{{ $.Scratch.Add "path" $element }}
{{ $.Scratch.Add "path" "/" }}
{{ if ne $element "" }}
{{ $.Scratch.Add "breadcrumb" (slice (dict "url" ($.Scratch.Get "path") "name" . "position" (add $index 2))) }}
{{ end }}
{{ end }}
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [{{ range $.Scratch.Get "breadcrumb" }}{{ if ne .position 1 }},{{ end }}{
"@type": "ListItem",
"position": {{ .position }},
"item": {
"@id": "{{ .url }}",
"name": "{{ .name }}"
}
}{{ end }}]
}
</script>
So I'm not sure what your list page template looks like, but for example in mine I had
{{ partial "header.html" . }}
when it should have been
{{ partial "header" . }}
This removed the repeating url's. I have all of the same code you have rendering in my header partial.