Articles

Generate an HTML Sitemap With Code Snippets

HTML sitemaps significantly enhance user experience by providing a clear, organized overview of a website’s content. It serves as a roadmap, making it easy for visitors to find specific pages or sections they’re looking for. This is especially beneficial for large or complex websites, where navigation menus might not be sufficient. By improving navigation and reducing the number of clicks required to reach desired content, HTML sitemaps keep users engaged and reduce frustration.

From an SEO perspective, HTML sitemaps contribute to improved search engine optimization by facilitating the discovery and indexing of web pages. While primarily designed for users, they also help search engine crawlers efficiently explore and understand the website’s structure. The internal links within the sitemap lead crawlers to all corners of the website, ensuring that even deep or recently added pages get indexed. This, in turn, can lead to better visibility in search engine results pages (SERPs) and potentially increased organic traffic.

This code snippet creates a function in WordPress to generate an HTML sitemap and provides a shortcode to display it. Here’s a breakdown of what each part does:

Post and Page Sitemap in 2 Columns

[code]
// Function to generate HTML sitemap
function generate_html_sitemap() {
// Get categories and posts
$categories = get_categories(array(
‘orderby’ => ‘name’,
‘order’ => ‘ASC’
));

// Get pages
$pages = get_pages(array(
‘sort_column’ => ‘post_title’,
‘sort_order’ => ‘ASC’
));

if (empty($categories) && empty($pages)) {
return ‘

No content found.

‘;
}

$html = ‘

‘;

$html .= ‘

‘;
$html .= ‘

Posts

‘;

foreach ($categories as $category) {
$html .= ‘

‘ . esc_html($category->name) . ‘

‘;
$html .= ‘

Join the conversation

Live conversation — moderated by AI, free to join, no signup