I’ve been searching for ways to build a sitemap for Kirby and found serveral solutions. The solutions I found didn’t really satisfy me because they all require a template and content folder which makes the sitemap show up in the panel. After playing around I came up with the following solution:
1 - Create a new sitemap.php snippet with the following contents:
<?= '<?xml version="1.0" encoding="utf-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($pages as $p): ?>
<?php if (in_array($p->uri(), $ignore) || $p->isInvisible()) continue ?>
<url>
<loc><?= html($p->url()) ?></loc>
<lastmod><?= $p->modified('c') ?></lastmod>
<priority><?= ($p->isHomePage()) ? 1 : number_format(0.5 / $p->depth(), 1) ?></priority>
</url>
<?php endforeach ?>
</urlset>
2 - Add the following route to the config.php file:
<?php
c::set('routes', array(
array(
'pattern' => 'sitemap.xml',
'action' => function() {
$pages = site()->pages()->index();
$ignore = array('error');
header::contentType('text/xml', 'UTF-8', true);
snippet('sitemap', compact('pages', 'ignore'));
}
),
array(
'pattern' => 'sitemap',
'action' => function() {
return go('sitemap.xml');
}
),
));
I thought I share it with you, hope you guys like it!