HUGO

How to Get Shortcode Variables From Pages Into sitemap.xml

Hello,

Currently Hugo’s built-in sitemap generator only creates two field attributes for images – image:loc and image:license. Unfortunately this does not tell Google Bots anything about the images for SEO. The fields, image:title & image:caption at minimum is needed with the other two for best practices; Otherwise it’s basically dead XML code.

I am working on a site that is image-content driven and since a short code is given in each page labeled as “Featured-Image” and contains the needed variables where I would like to extract/pass into the sitemap.

Here is an example of the shortcode within a page.md:

{{< 
    featured-image 
    name          ="THE_IMAGE_NAME.jpg"
    featuredTitle ="THE_IMAGE_TITLE"
    location      ="THE_IMAGE_LOCATION"
    alt           ="THE_IMAGE_ALT/CAPTION"
>}}

Within the sitemap.xml is what I would like to do:

    {{ $license := .Site.Params.schema.license }}
    {{ range .Data.Pages }}
    ...
      {{ range $i := .Resources.ByType "image" }}

      // Below is the additional data I am wanting to get from the pages' 
      // shortcode named "featured-image".

      {{ $caption   := .Get "alt" }}
      {{ $location  := .Get "location"}}
      {{ $title     := .Get "featuredTitle"}}

      <image:image>
         <image:loc>{{ $i.Permalink }}</image:loc>
         <image:license>{{ $license }}</image:license>

          // Below are the additional attributes needed:

          <image:geo_location>{{ $location }}</image:geo_location>
          <image:title>{{ $title }}</image:title>
          <image:caption>{{ $caption }}</image:caption>
      </image:image>
    ...

Any help with code example will be greatly appreciated!
~jason

Hello @Jason_Kennedy

What is the output for your code right now? is it throwing an error? Please share more details, so we can digging into it.

Hello @Mehedi

Theres no errors; just empty data in the sitemap.xml

e.g.:

        <image:loc>https://some.org/articles/drive/image-name.jpg</image:loc>
        <image:caption></image:caption>
        <image:geo_location></image:geo_location>
        <image:title></image:title>
        <image:license>https://some.org/Image-licensing</image:license>
    </image:image>

1st Put the values in frontmatter, outside of the shortcode this values are not visible.

2nd you use range inside a range,

 {{ range .Data.Pages }}
      {{ $p  := . }}
    ...
      {{ range $i := .Resources.ByType "image" }}


         {{ $location  := $p.Get "location"}}

the dot is alway the element of the inner range
HTH