-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsitemap.php
More file actions
58 lines (51 loc) · 2.12 KB
/
sitemap.php
File metadata and controls
58 lines (51 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
declare(strict_types=1);
/**
* FlatFile Blog - Enhanced XML Sitemap
* Generates comprehensive XML sitemap for search engines
*/
require_once 'functions.php';
// Get all published posts
$posts = get_posts_by_status('published');
// Set XML headers with caching
header('Content-Type: application/xml; charset=utf-8');
header('Cache-Control: public, max-age=3600'); // 1 hour cache
$index_file = CONTENT_DIR . 'index.json';
$last_modified_ts = file_exists($index_file) ? filemtime($index_file) : time();
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $last_modified_ts) . ' GMT');
// Generate XML sitemap
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<!-- Homepage -->
<url>
<loc><?php echo BASE_URL; ?></loc>
<lastmod><?php echo date('c'); ?></lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<!-- RSS feed -->
<url>
<loc><?php echo BASE_URL; ?>rss</loc>
<lastmod><?php echo date('c'); ?></lastmod>
<changefreq>daily</changefreq>
<priority>0.6</priority>
</url>
<!-- Individual posts -->
<?php foreach ($posts as $post): ?>
<url>
<loc><?php echo BASE_URL; ?><?php echo urlencode($post['slug']); ?></loc>
<lastmod><?php echo date('c', strtotime($post['updated'])); ?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.9</priority>
<?php if (!empty($post['meta']['image'])): ?>
<image:image>
<image:loc><?php echo htmlspecialchars($post['meta']['image']); ?></image:loc>
<image:title><?php echo htmlspecialchars($post['title']); ?></image:title>
<image:caption><?php echo htmlspecialchars($post['excerpt'] ?: substr(strip_tags($post['content_markdown'] ?? ''), 0, 200)); ?></image:caption>
</image:image>
<?php endif; ?>
</url>
<?php endforeach; ?>
</urlset>