I created the a breadcrumb list partial for my site and put it in the baseof page, i then use the site params in the configuration file to list a few predefined urls but now id like to have breadcrumb auto generated for certain taxonomies, tags, etc…
Where can i place the code so that when the site is being built all the breadcrumb are generated automatically?
Breadcrumbs are usually placed in the head or just before the closing body tag. So, that would still be your baseof.html file. However, you will need to use conditionals to target the different sections of your site (home, page, taxonomy, list, etc).
{{- define "breadcrumb" -}}
{{- $home := .Site.GetPage "/" -}}
{{- if $home -}}
{{- $homePosition := 1 -}}
{{- range $.Site.Params.breadcrumb_position -}}
{{- $split := split . ":" -}}
{{- if eq (index $split 0) "/" -}}
{{- $homePosition = int (index $split 1) -}}
{{- end -}}
{{- end -}}
{
"@type": "ListItem",
"position": {{ $homePosition }},
"item": {
"@id": "{{ $home.Permalink }}",
"name": "{{ $home.Title }}"
}
},
{{- end -}}
{{- $maxPosition := 1 -}}
{{- $positionedPages := slice -}}
{{- $allPages := .Site.RegularPages -}}
{{- range $.Site.Params.breadcrumb_position -}}
{{- $split := split . ":" -}}
{{- $posNum := int (index $split 1) -}}
{{- if gt $posNum $maxPosition -}}
{{- $maxPosition = $posNum -}}
{{- end -}}
{{- range $allPages -}}
{{- if eq (index $split 0) .RelPermalink -}}
{{- $positionedPages = $positionedPages | append . -}}
{
"@type": "ListItem",
"position": {{ $posNum }},
"item": {
"@id": "{{ .Permalink }}",
"name": "{{ .Title }}"
}
},
{{- end -}}
{{- end -}}
{{- end -}}
{{- range $allPages -}}
{{- if not (in $positionedPages .) -}}
{{- $maxPosition = add $maxPosition 1 -}}
{{- if not (in $.Site.Params.breadcrumb_excludes .RelPermalink) -}}
{
"@type": "ListItem",
"position": {{ $maxPosition }},
"item": {
"@id": "{{ .Permalink }}",
"name": "{{ .Title }}"
}
},
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
I use these fields defined in the config.toml:
breadcrumb_position = [“/:1”, “/home:1”, “/posts:2”, “/contact:3”, “/about:4”]
breadcrumb_excludes =
I defined that code above that will go through the entire site and generate the breadcrumbs.
One issue is that the / code doesn’t work and I have to define a separate function at the top to sort of getpage to get the homepage url.
is the homepage not defined in site.regularpages?
I’d like to grab every page and just generate the breadcrumbs based on whats defined in the breadcrumb_positions array and the breadcrumb_excludes array and everything else is included.