70 lines
2.2 KiB
Markdown
70 lines
2.2 KiB
Markdown
|
|
---
|
||
|
|
title: Creating An Automatically Updating Sitemap In PureBlog
|
||
|
|
slug: creating-an-automatically-updating-sitemap-in-pureblog
|
||
|
|
date: 2026-09-20 14:38
|
||
|
|
status: published
|
||
|
|
tags: [tech, tutorial, meta]
|
||
|
|
description:
|
||
|
|
---
|
||
|
|
|
||
|
|
I wanted to not have to update my sitemap every time I made a new page, so here's my VERY basic automatically updating Sitemap page for Pureblog. I altered <a href="https://dcblog.dev/create-a-list-of-items-from-a-folder-using-scandir">THIS CODE SNIPPET</a> by David Carr for it.
|
||
|
|
|
||
|
|
Basically, this takes a list of the files in your pureblog/content/pages directory and retrieves it for the page with its title. Note that I'm using my dockerized version so please edit accordingly. When I learn more PHP, I will be updating this with categories and alphabetical sorting. For now though:
|
||
|
|
|
||
|
|
1) Make a file called sitemap-auto.php in content/includes/
|
||
|
|
|
||
|
|
2) Make a custom route in settings
|
||
|
|
|
||
|
|
``` /sitemap-auto | /content/includes/sitemap-auto.php ```
|
||
|
|
|
||
|
|
3) Copy the code below <a href="https://treasurechest.alien.town/agnes/misc-etc/src/branch/main/automatically-updating-pureblog-sitemap.php">or here</a>
|
||
|
|
|
||
|
|
```<?php
|
||
|
|
|
||
|
|
require PUREBLOG_BASE_PATH . '/includes/header.php';
|
||
|
|
render_masthead_layout($config, ['page' => null]);
|
||
|
|
|
||
|
|
?>
|
||
|
|
|
||
|
|
<main>
|
||
|
|
<h1> Sitemap</h1>
|
||
|
|
|
||
|
|
<?php
|
||
|
|
|
||
|
|
$directory = '/var/www/html/content/pages/';
|
||
|
|
|
||
|
|
$files = scandir($directory);
|
||
|
|
|
||
|
|
echo "<ul>";
|
||
|
|
|
||
|
|
foreach ($files as $file) {
|
||
|
|
if ($file !== "." && $file !== "..") {
|
||
|
|
$name = pathinfo($file, PATHINFO_FILENAME);
|
||
|
|
$path = $directory . '/' . $file;
|
||
|
|
$contents = file_get_contents($path);
|
||
|
|
|
||
|
|
preg_match('/^title:\s*(.+)$/m', $contents, $matches);
|
||
|
|
|
||
|
|
$title = $matches[1] ?? pathinfo($file, PATHINFO_FILENAME);
|
||
|
|
|
||
|
|
$url = '/' . pathinfo($file, PATHINFO_FILENAME);
|
||
|
|
|
||
|
|
echo '<li>🛸 <a href="' . htmlspecialchars($url) . '">'
|
||
|
|
. htmlspecialchars(trim($title))
|
||
|
|
. '</a></li>';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
echo "</ul>";
|
||
|
|
?>
|
||
|
|
|
||
|
|
</main>
|
||
|
|
<?php render_footer_layout($config, ['page' => null]); ?>
|
||
|
|
</body>
|
||
|
|
</html>
|
||
|
|
```
|
||
|
|
4) Put it in /content/includes/sitemap-auto.php
|
||
|
|
|
||
|
|
You're done :)
|
||
|
|
|
||
|
|
This will automatically update when a new file is in the pages list. You can find a live preview on my site <a href="https://agnes.love/sitemap-auto">here.</a>
|