Sitemaps - Part 04
**Source path:** Spiralist/wp-content/plugins/ns12-manuscript/includes/sitemaps.php
<a href="<?php echo esc_url(spiralist_book_pages_sitemap_localized_route_url('sitemap.xml', $language)); ?>" hreflang="<?php echo esc_attr((string) ($language_data['hreflang'] ?? $language)); ?>"><?php echo esc_html((string) ($language_data['label'] ?? strtoupper($language))); ?> XML</a>
<?php endforeach; ?>
</div>
</header>
<?php foreach ($groups as $group => $group_entries) : ?>
<section>
<h2><?php echo esc_html($group); ?></h2>
<?php foreach ($group_entries as $entry) : ?>
<article class="entry">
<h3><?php echo esc_html((string) ($entry['title'] ?? 'Untitled')); ?></h3>
<?php if (!empty($entry['description'])) : ?>
<p><?php echo esc_html(wp_trim_words(wp_strip_all_tags((string) $entry['description']), 28, '')); ?></p>
<?php endif; ?>
<div class="links" aria-label="<?php echo esc_attr((string) ($entry['title'] ?? 'Entry') . ' language links'); ?>">
<?php foreach ($languages as $language => $language_data) : ?>
<?php $url = (string) ($entry['urls'][$language] ?? ''); ?>
<?php if ($url === '') { continue; } ?>
<a href="<?php echo esc_url($url); ?>" hreflang="<?php echo esc_attr((string) ($language_data['hreflang'] ?? $language)); ?>"><?php echo esc_html((string) ($language_data['label'] ?? strtoupper($language))); ?></a>
<?php endforeach; ?>
</div>
</article>
<?php endforeach; ?>
</section>
<?php endforeach; ?>
</main>
</body>
</html>
<?php
}
function spiralist_book_pages_maybe_render_sitemap(): void
{
$sitemap = sanitize_key((string) get_query_var('spiralist_book_pages_sitemap', ''));
if ($sitemap === '') {
return;
}
if ($sitemap === 'xml') {
spiralist_book_pages_render_xml_sitemap();
exit;
}
if ($sitemap === 'html') {
spiralist_book_pages_render_html_sitemap();
exit;
}
}
add_action('template_redirect', 'spiralist_book_pages_maybe_render_sitemap', -100);
function spiralist_book_pages_add_sitemap_to_robots(string $output, bool $public): string
{
if (!$public) {
return $output;
}
$sitemap_line = 'Sitemap: ' . home_url('/sitemap.xml');
if (strpos($output, $sitemap_line) !== false) {
return $output;
}
return rtrim($output) . "\n" . $sitemap_line . "\n";
}
add_filter('robots_txt', 'spiralist_book_pages_add_sitemap_to_robots', 10, 2);