JSON-LD Schema on Author Profile Pages - cannot target author term page in site-schema partial

Hi, I have set up author profile pages using term list templates and author taxonomy.
I cannot get the JSON from my site-schema.html partial to render on an author profile page.
The schema will output on every other pages I have tried.
Eventually, to help debug, I tried to target the specific author page (did not work) and even put some JSON on the term list template itself (did work).

I must be missing something about taxonomy term templates!

Please help!

Repo: GitHub - jemmsuk/camino: Repo for Hugo files for caminosantiagocompostela.com new static site

Environment:
$ hugo env
hugo v0.121.2-6d5b44305eaa9d0a157946492a6f319da38de154+extended windows/amd64 BuildDate=2024-01-05T12:21:15Z VendorInfo=gohugoio
GOOS=“windows”
GOARCH=“amd64”
GOVERSION=“go1.21.5”
github. com / sass/libsass=“3.6.5”
github. com / webmproject/libwebp=“v1.3.2”

This line looks fishy to me

   "email": {{ $email }},

In JSON, strings must be enclosed in double quotes. You do that for the other values but not for $email.

1 Like

Great catch, thanks. It’s in the part that is working, but I corrected it to find out if it had other effects. Issue still present after correcting this at line 151 of site-schema.html.

I’m pretty sure there is something wrong with my if else statements in site-schema.html. Still working on it!

OK, so I created a new schema partial just for the Profile pages layouts/partials/profilePage-schema.html and called this from head.html partial so basically this logic works {{ if eq .Type “authors” }}.

Once I put the entire contents back into the site-schema.html partial and commented out the new profilePage-schema.html, then it stopped working again.

So I guess there is some issue with my {{if else}} statements in the site-schema.html page. I would appreciate if someone has the time to have a quick look at that. Otherwise I do have this workaround

Why not add some warnf statements to the code to let you see the flow of control when the pages are built?

1 Like

Thanks for the suggestion, I’ll look into it. fmt.Warnf | Hugo

In the end I used Chat GPT to help troubleshoot and verified results with JSON-LD Playground

I continued to use the separate profilePage-schema.html file for simplicity and followed these Google guidelines Znaczniki schema.org na stronie profilu (ProfilePage) | Centrum wyszukiwarki Google  |  Documentation  |  Google for Developers

I ended up with the following as working, there may be a more elegant solution, but here it is for now.
Thanks for your input @chrillek

{{ if eq .Type "authors" }}
        {{ $author := .Params.name }}
        {{ $email := .Params.email }}
        {{ $email := .Params.email }}
        {{ $img := .Params.photo }}
        {{ $role := .Params.role }}
        {{ $sameas := slice }}
        {{ if .Params.linkedin }}
            {{ $sameas = $sameas | append .Params.linkedin }}
        {{ end }}
        {{ if .Params.twitter }}
            {{ $sameas = $sameas | append .Params.twitter }}
        {{ end }}
        <script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "ProfilePage",
            "dateCreated": "2019-12-23T12:34:00-05:00",
            "dateModified": "2019-12-26T14:53:00-05:00",
            "mainEntity": {
                "@type": "Person",
                "name": "{{ $author }}",
                "jobTitle": "{{ $role }}",
                "email": "{{ $email }}",
                "sameAs": "{{ $sameas | jsonify }}",
                "url": "{{ .Permalink | absURL }}",
                "image": "{{ path.Join .Permalink $img | absURL }}"
            },   

            "hasPart": [
                
                {{- $total := len .Data.Pages -}}
                {{- range $index, $page := .Data.Pages -}}
                {{- $isLast := eq (add $index 1) $total -}}
                {{- if and (not .IsHome) (not (eq .Slug "articles")) -}} <!-- Exclude posts-->
                {{ $title:= .Params.Title }}  

            {
                "@type": "Article",
                "headline": "{{ $title }}",
                "url": "{{ .Permalink }}",
                {{ if $f_img := .Params.featured_image }}
                "image": "{{ path.Join .Permalink $f_img | absURL }}",
                {{ end }}
                {{ with .PublishDate }}
                {{ $publishdateMachine := . | time.Format "2006-01-02T15:04:05Z07:00" }}
                "datePublished": "{{ $publishdateMachine }}",
                {{ end }}
                {{ with .Lastmod }}
                {{ $updatedateMachine := . | time.Format "2006-01-02T15:04:05Z07:00" }}
                "dateModified": "{{ $updatedateMachine }}",
                {{ end }}
                
                
                "author": { 
                    "name": "{{ $author }}",
                    "sameAs": "{{ $sameas | jsonify }}",
                    "url": "{{ $.Permalink | absURL }}"
                            }                        
                }
                

            {{- if not $isLast -}}
                        ,
            {{- end -}}
                        
            {{ end }}
            {{ end }}
              ] 
        }
        </script>
    {{ end }}

Perhaps it’s easier and less error-prone to build a dict in Hugo first and then output it with jsonify?

1 Like

Something else I will look at. New to Hugo so finding my way around. Thanks again @chrillek for the suggestion collections.Dictionary | Hugo.

I’m trying to use variables everywhere to make it easier to use the same template over and over with a few sites i’m moving off Wordpress and Drupal. Hopefully I will only be doing the heavy lifting once (famous last words, as we say in UK :laughing:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.