My content is like this:
- /content/sectionA/_index.md
- /content/sectionA/subsection1/_index.md
- /content/sectionA/subsection1/my-post.md
- /content/sectionA/subsection2/_index.md
- /content/sectionA/subsection2/my-second-post.md
This means that a single post can belong to two sections: the main section (/content/sectionA/) and its ‘subsection’ (/content/sectionA/subsectionA/).
And so for the breadcrumbs JSON-LD format I have up to four levels:
- The homepage,
- The main section page,
- The ‘subsection’ page (if applicable),
- And the post itself.
How can I make this in Hugo?
Here’s what I have so far:
<!-- Breadcrumbs for single post pages -->
{{ if and (ne .Section "") .IsPage }}
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [{
"@type": "ListItem",
"position": 1,
"name": "Home",
"item": "{{ .Site.BaseURL }}"
},{
"@type": "ListItem",
"position": 2,
"name": "{{ .CurrentSection.Parent.Title }}",
"item": "{{ .CurrentSection.Parent.Permalink }}"
},{
"@type": "ListItem",
"position": 3,
"name": "{{ .CurrentSection.Title }}",
"item": "{{ .CurrentSection.Permalink }}"
},{
"@type": "ListItem",
"position": 4,
"name": "{{ .Title }}",
"item": "{{ .Permalink }}"
}]
}
</script>
{{ else }}
<!-- Breadcrumbs for main section -->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [{
"@type": "ListItem",
"position": 1,
"name": "Home",
"item": "{{ .Site.BaseURL }}"
},{
"@type": "ListItem",
"position": 2,
"name": "{{ .Title }}",
"item": "{{ .Permalink }}"
}]
}
</script>
{{ end }}
Problem is that this code does not work for ‘subsection’ pages, since the else
code block kicks into gear for those pages (while it should not).
I know there’s a .Sections
variable that lists all the ‘subsections’ of a particular page. But I need to look the other way, and get all sections that a particular page belongs to.
Things I’ve failed with:
- Split the permalink and derive sections that way.
- Use
.Site.GetPage
to fetch pages. - Build up a custom dictionary with
dict
, - And more.