Hugo custom sitemap.xml

Select which pages will be in sitemap.xml

Hugo custom sitemap.xml

Hugo is a wonderful tool to generate static web sites. Out of the box, it generates a sitemap.xml with all the generated pages in it. But, how to hide a page from sitemap.xml in Hugo? It is very easy, and the official documentation is clear.

Just create a custom sitemap.xml implementation in the following path:

layouts/_default/sitemap.xml

{{-  printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:xhtml="http://www.w3.org/1999/xhtml">
  {{- range .Data.Pages }}
      {{- if not .Params.noSitemap }}
  <url>
    <loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
    <lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
    <changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
    <priority>{{ .Sitemap.Priority }}</priority>{{ end }}
      {{- if .IsTranslated }}
        {{- range .Translations }}
    <xhtml:link rel="alternate" hreflang="{{ .Lang }}" href="{{ .Permalink }}" />
        {{- end }}
    <xhtml:link rel="alternate" hreflang="{{ .Lang }}" href="{{ .Permalink }}" />
      {{- end }}
  </url>
      {{- end }}
  {{- end }}
</urlset>

To hide a page from sitemap.xml, just add the noSitemap flag to the frontmatter of the page.

content/page.md

---
title: "Title"
noSitemap: true
---

Reference:

gohugo.io/templates/sitemap-template