Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgarrant/bef9f8c925412ea8d7677c639013d38e to your computer and use it in GitHub Desktop.
Save bgarrant/bef9f8c925412ea8d7677c639013d38e to your computer and use it in GitHub Desktop.
Automatic Sitemap XML Generator for Statamic CMS Pages and Mounted Collections

How to automatically generate a Sitemap XML file using Statamic CMS.

This method will work for all Pages and Mounted Collections (just like Pages view in CP).

  1. Create a template called sitemap.html and add it to your theme/layouts/ folder.
  2. Add the following code to the new template
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{ nav from="/" include_home="true" include_entries="true" exclude="our-team" }}
  <url>
    <loc>{{ permalink }}</loc>
    <lastmod>{{ last_modified format="Y-m-d" }}</lastmod>
    <changefreq>monthly</changefreq>
    <priority>{{ if url == homepage }}0.8{{ else }}0.5{{ /if }}</priority>
  </url>
  {{ if children }}
  {{ children sort="order" }}
  <url>
    <loc>{{ permalink }}</loc>
    <lastmod>{{ last_modified format="Y-m-d" }}</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.5</priority>
  </url>
  {{ /children }}
  {{ /if }}
{{ /nav }}
</urlset>
  1. Open up site/settings/routes.yaml and add the following:
routes:
  /sitemap:
    template: sitemap
    layout: sitemap
    content_type: xml
  1. Visit the page at http://yourdomain/sitemap
@nelo-digitalagentur
Copy link

Suggestion: Add the following to the top so the correct xml header would be rendered, too

{{ xml_header }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment