How to create sitemap.xml? #1854
Replies: 16 comments
|
Hi @rolandtoth. I’ve created one for my (yet un-launched) site. You can use the following template (this uses liquid, but the underlying idea will work regardless of template format): ---
permalink: /sitemap.xml
---
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- for item in collections.all %}
{%- unless item.data.ignore == true %}
<url>
<loc>https://example.com{{ item.url }}</loc>
<lastmod>{{ item.date | date: '%Y-%m-%d' }}</lastmod>
<changefreq>{{ item.data.changefreq }}</changefreq>
<priority>{{ item.data.priority }}</priority>
</url>
{%- endunless %}
{%- endfor %}
</urlset>You’ll need to create a collection (I’ve used {
"layout": "layouts/article",
"permalink": "{{ page.date | date: '%Y/%m' }}/{{ page.fileSlug }}.html",
"changefreq": "monthly",
"priority": "0.9"
}For single pages, or if you need to override the above in a particular instance, just set the same values in your frontmatter, like so: ---
title: 'My great page'
permalink: /path/to/index.html
changefreq: weekly
priority: 1
---
Hello, and welcome to my great page!I also allow pages to be excluded from the sitemap if they have the value Hope this helps! |
|
Thanks @paulrobertlloyd , this was really useful. I made some minor modifications (added my own filters and adjusted to nunjucks, plus added an xsl stylesheet): |
|
Thanks, this looks really useful too! Possibly a good candidate for the tutorials section. |
|
@rolandtoth I'm curious what your Why not do |
|
It's a filter I created for nunjucks so that if the variable I apply on doesn't exist then it will try using alternative values. I tried using your suggestion but the build failed ("Template render error"). module.exports = function () {
for (arg of arguments) {
if (arg) {
return arg;
}
}
return false;
};It's not too sophisticated but so far so good. It accepts multiple values, eg. for the SEO description I use something like this: So if the frontmatter seo_description is not set, it will try other frontmatter data (excerpt and title) and outputs the first that has value. |
|
Oops, I got my syntax wrong. What I wanted to suggest was this: For your case I'm pretty sure you could do: |
|
@zachleat is I'd assumed that since collections group things together by tag, that a tag would be required for it to ever appear. The |
|
Thanks for the "var1 or var2" syntax, it works. However, if you apply a filter on it, you need to put them in parenthesis, otherwise the filter will be applied to the last item only.
|
|
Here’s how I did it for v8.dev: 11ty/eleventy-base-blog#22 (before I saw this thread) |
|
@rolandtoth @paulrobertlloyd @mathiasbynens per 11ty/buildawesome#253, if you're using pagination, are pages that are generated showing up in your sitemap? I found |
|
Sorry, I don't have pagination in any project where I have sitemap :) Update: just added a sitemap and pages like /page-3/, /page-4/, etc are not included. |
|
@edwardhorsford per your comment here #1853 (comment) it looks like no! I’ve filed #21 |
|
@rolandtoth ah, you might have gotten bit by 11ty/buildawesome#253? Can you follow along over there? |
|
This repository is now using lodash style issue management for documentation requests. This means documentation issues will now be closed instead of leaving them open. View the documentation queue backlog here. Don’t forget to upvote the top comment with 👍! |
|
@zachleat Where would you like to see this documented? I assume, in the docs folder of https://github.com/11ty/11ty.io ? |
Additionally, pages generated dynamically such as tag pages also seem to be hit or miss when it comes to the sitemap. |
Uh oh!
There was an error while loading. Please reload this page.
Could you provide some hint on how to create a sitemap.xml automatically?
All reactions