BreadcrumbList JSON-LD Schema.org structured data

Hi guys,
I want to create code for pagination - BreadcrumbList via Schema.org. It should be automatically build for every single page with one block of code.
So I adjusted hugo breadcrumb navigation from this example in Hugo doc like this:

Input:

<script type="application/ld+json">
{ 
{{ template "breadcrumbnav" (dict "p1" . "p2" .) }}
{{ define "breadcrumbnav" }}
{{ $i := 1 }}
{{ if .p1.Parent }}
{{ template "breadcrumbnav" (dict "p1" .p1.Parent "p2" .p2 )  }}
{{ else if not .p1.IsHome }}
{{ template "breadcrumbnav" (dict "p1" .p1.Site.Home "p2" .p2 )  }}
{{ end }}
    {
        "@type": "ListItem",
        "position": {{ $i }},
        "item": 
        {
            "@id": "{{ .p1.Permalink }}",
            "name": "{{ if .p1.IsHome }}Home{{ else }}{{ .p1.Title }}{{ end }}"
        }       
    },
{{ end }}
}
</script>

Output:

<script type="application/ld+json">
{ 
    {
        "@type": "ListItem",
        "position": 1,
        "item": 
        {
            "@id": "http://localhost:1313/",
            "name": "Home"
        }       
    },
    {
        "@type": "ListItem",
        "position": 1,
        "item": 
        {
            "@id": "http://localhost:1313/category-tesla/",
            "name": "Tesla Motors"
        }       
    },
    {
        "@type": "ListItem",
        "position": 1,
        "item": 
        {
            "@id": "http://localhost:1313/category-tesla/model-3/",
            "name": "New Tesla Model 3"
        }       
    },
}
</script>

I have 2 issues I cant resolve.

  1. how to create counter for key “position” if there is not range function? Now is {{ $i }} but it is valid only for first position and there has to be something like add $i 1 for another loop.
  2. how to remove , in last iteration (I mean , after penultimate })

Link to example on GitHub

If we solve those issues, this thread could help others who wants to implement BreadcrumbList pagination.

Thank you.
Zde

1 Like

Ahem…

You mean dynamic, and not dynamical. You’d like your breadcrumbs generated each time, but you would not like the method by which it is generated to change. :wink:

Sorry, my friends and I have been on a words kick lately. Good luck on getting help! :slight_smile:

Well, I mean “dynamic” = one code valids for all url. Yes, not really good word in this case (remove from title…).
All (two or three) BreadcrumbList solutions in discourse.gohugo.io don’t meet requirements.

For example, this one just cut .Page url and creates item - name from that url. It doesn’t reflect real breadcrumb navigation on page.
So you cannot have different item url and item name (as is usuall).
Also if you use short url, this method is useless (i.e. if you use url: yourdomain.com/product-name instead of long url yourdomain.com/category/subcategory/product-name ).
So I just hoped to get here some working general method.

for the counter use .Scratch


and
Math

Answer to your question 2:

{{ range $i, $e := . }}
  {{ if $i }}/{{ end }}
  <a href="{{ "tags/" | absURL }}{{ $e | urlize }}" rel="tag">{{ $e }}</a>
{{ end }}

The second line will add a slash, but ONLY if we are at item 2 and following. You could reuse that for your range. But look at the range command. The $i is an integer starting at 0 that goes up each iteration.

1 Like

@ju52 @davidsneighbour thansk for reply. I use couple of counters and symbol removing codes on my websites but only together with range.
In breadcrumbnav code I dont know what cause that loop and how to detect template breadcrumbnav on the first position and after template breadcrumbnav on the second position etc. This is probably the main problem.

Hi guys,
I paid to one programmer guy (symbolic amount of money). In cooperation with him, we finaly found good solution for automatic breadcrumbList ld+json. I share it with Hugo community.
Resulting code is very robust and applicable for most of situations.

Find entire example project on github.

Main Code Features

  • automatically generated breadcrumbList for all pages at your website
  • working for multi language sites
  • working for custom url
  • creates item name from .Title or optional item name from front matter params BreadcrumbListName
2 Likes