[solved] Next/PrevInSection don't work if "section" in disableKinds?

I’m confused, clearly.

Backstory:

  • I don’t want “/posts/” or “/pages/” to show up in the sitemap.xml. My understanding was that I should add “sections” to the “disableKinds” setting.
  • I have posts in content/posts/{postname}.md files. I assumed I could still access .PrevInSection, etc., to do previous/next post pagination.

It appears that if “sections” is in “disableKinds”, then these vars are always falsey. =(

Can I not have both? Did I do it wrong?

disableKinds = sections disables all sections I believe.

If you want to exclude something from the sitemap you will probably need to add a custom template.

Thanks @royce for confirming.

I have since solved the issue by using a custom sitemap template. I’m posting details here, for anyone that may stumble into the same situation.

I initially wanted to avoid creating a custom template for the sitemap, because I was worried I would get something wrong – and I hate to override anything unless absolutely necessary.

Thankfully, the default template that Hugo uses is listed here: https://gohugo.io/templates/sitemap-template/#hugo-s-sitemap-xml

Since I currently list my blog posts on the “home” page only, I only needed that page, and actual URLs for two sections (“posts”, “pages”) in the sitemap. (As mentioned in the original posts, the list pages for those are what I wanted to remove from the sitemap).

Here’s the sitemap.xml template I came up with:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  {{ range where .Data.Pages "Section" "pages" }}
      {{ if .IsPage }}
      <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 }}
      </url>
      {{ end }}
  {{ end }}

  {{ range where .Data.Pages "Section" "blog" }}
  {{ if .IsPage }}
  <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 }}
  </url>
  {{ end }}
  {{ end }}

  {{ with .Site.GetPage "home" }}
  <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 }}
  </url>
  {{ end }}
</urlset>

Essentially, I’m manually looping over the specific posts in the sections I care about, and putting those URLs in the sitemap.

I’d much rather have had the option to exclude certain URLs from the sitemap, but this will do for now!