How to Create HTML Sitemap in WordPress Without a Plugin

There are lots of HTML sitemap plugins available for WordPress. But in this blog, I will show you how to create HTML sitemap dynamically in WordPress without using a plugin. Dynamically means that the sitemap updates automatically with every new post and other blog changes.

Here the steps to create HTML sitemap in WordPress dynamic,

Step 1:

Create a file called page-sitemap.php in your WordPress theme folder.

Sample file path: <WordPress Installed Root Folder>/wp-content/themes/<Your Theme>/page-sitemap.php

Step 2:

Add the following code in the newly created file (page-sitemap.php),

<?php
/**
 * Template Name: HTML Sitemap Page
 *
 * Create Dynamic HTML Sitemap in WordPress
 */

get_header(); 
?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

            <div class="html-sitemap">
                
                <h2>Author(s):</h2>
                <ul class="sitemap-authors">
                    <?php wp_list_authors('exclude_admin=1&optioncount=1'); ?>
                </ul>
         
                <h2>Pages:</h2>
                <ul class="sitemap-pages">
                    <?php wp_list_pages(array('exclude' => '', 'title_li' => '')); // Exclude pages by ID ?>
                </ul>

                <h2>Posts:</h2>
                <ul>
                    <?php
                        $categories = get_categories('exclude='); // Exclude categories by ID
                        foreach ($categories as $cat) {
                        ?>
                            <li class="category">
                                <h3><span class="grey">Category: </span><?php echo $cat->cat_name; ?></h3>
                                <ul class="cat-posts">
                                <?php
                                    query_posts('posts_per_page=-1&cat='.$cat->cat_ID); //-1 shows all posts per category. 1 to show most recent post.
                                    while(have_posts()): 
                                        the_post();
                                        $category = get_the_category();
                                        if ($category[0]->cat_ID == $cat->cat_ID) { ?>
                                            <li>
                                                <?php the_time('M d, Y')?> &raquo; <a href="<?php the_permalink() ?>"  title="<?php the_title(); ?>"><?php the_title(); ?></a> (<?php comments_number('0', '1', '%'); ?>)
                                            </li>
                                        <?php 
                                        }
                                    endwhile;
                                ?>
                                </ul> 
                            </li>
                    <?php } ?>
                </ul>
                <?php  wp_reset_query(); ?>

                <h2>Archives:</h2>
                <ul class="sitemap-archives">
                    <?php wp_get_archives('type=monthly&show_post_count=true'); ?>
                </ul>
            </div>

        </main>
    </div>

<?php
get_sidebar();
get_footer();

Step 3:

Then, login to your WordPress admin panel, and on the left sidebar, go to Pages > Add New.

This will create a new page as shown in the image below:

How to Create HTML Sitemap in WordPress Without a Plugin

Here, enter the page title like Sitemap, In the Page Attributes section, choose the HTML Sitemap Page template and then click Publish. Now the sitemap page will be created and you can check it in the frontend.

Hope this helps.

4 Comments on “How to Create HTML Sitemap in WordPress Without a Plugin”

    1. Thank you for the code as well. I was able to get this sitemap working on my website. I had to change the owner of the sitemap.php to match the owner of my WordPress files.

      Also, I had to change the group to match the group account for the WordPress files. I did not receive a 500 error. For example:

      chown user sitemap.php
      chgrp user sitemap.php

      Aaron

  1. Hi there, I’ve tested your code and it works. Thanks so much! But there are some issues. One small issue I have is how do I exclude particular pages or posts? For example, I would like to exclude the Login page.

    My second issue is that my blog sidebar is positioned all the way at the bottom of the page on the Sitemap page, is there a way I can remove the blog sidebar for this page? I would like to refrain from using display: none; if possible

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *