agnes-dot-love-pureblog/content/autosaves/post-creating-an-automatically-updating-sitemap-in-pureblog.json
2026-11-17 22:01:10 -05:00

1 line
No EOL
2.4 KiB
JSON

{"timestamp":1791058400,"title":"Creating An Automatically Updating Sitemap In PureBlog","content":"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.\r\n\r\nBasically, 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:\r\n\r\n1) Make a file called sitemap-auto.php in content\/includes\/\r\n\r\n2) Make a custom route in settings\r\n\r\n``` \/sitemap-auto | \/content\/includes\/sitemap-auto.php ```\r\n\r\n3) Copy the code below <a href=\"https:\/\/treasurechest.alien.town\/agnes\/misc-etc\/src\/branch\/main\/automatically-updating-pureblog-sitemap.php\">or here<\/a>\r\n\r\n```<?php \r\n\r\nrequire PUREBLOG_BASE_PATH . '\/includes\/header.php';\r\nrender_masthead_layout($config, ['page' => null]);\r\n\r\n?>\r\n\r\n<main>\r\n<h1> Sitemap<\/h1>\r\n\r\n<?php\r\n\r\n$directory = '\/var\/www\/html\/content\/pages\/';\r\n\r\n$files = scandir($directory);\r\n\r\necho \"<ul>\";\r\n\r\nforeach ($files as $file) {\r\n if ($file !== \".\" && $file !== \"..\") {\r\n $name = pathinfo($file, PATHINFO_FILENAME);\r\n $path = $directory . '\/' . $file;\r\n $contents = file_get_contents($path);\r\n\r\n preg_match('\/^title:\\s*(.+)$\/m', $contents, $matches);\r\n\r\n $title = $matches[1] ?? pathinfo($file, PATHINFO_FILENAME);\r\n\r\n $url = '\/' . pathinfo($file, PATHINFO_FILENAME);\r\n\r\n echo '<li>🛸 <a href=\"' . htmlspecialchars($url) . '\">'\r\n . htmlspecialchars(trim($title))\r\n . '<\/a><\/li>';\r\n}\r\n}\r\necho \"<\/ul>\";\r\n?>\r\n\r\n<\/main>\r\n<?php render_footer_layout($config, ['page' => null]); ?>\r\n<\/body>\r\n<\/html>\r\n```\r\n4) Put it in \/content\/includes\/sitemap-auto.php\r\n\r\nYou're done :)\r\n\r\nThis 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>","description":"","status":"published","tags":"tech, tutorial, meta","date":"2026-09-20 14:38","layout":""}