How to Create HTML sitemap without a plugin in WordPress

Create HTML sitemap without a plugin

Creating an automatically updating HTML sitemap without a plugin in WordPress would typically involve implementing a custom solution using PHP. However, it’s important to note that this approach may require coding knowledge, and making changes to your theme files should be done with caution, considering potential impacts on your site’s functionality.

Here is a basic example of how you might approach this:

  1. Edit Your Theme’s functions.php File:
    • In your WordPress dashboard, go to “Appearance” and then “Theme Editor.”
    • Find and click on the functions.php file on the right sidebar.
  2. Add Custom PHP Code:
function generate_html_sitemap() {
    $pages = get_pages();
    $posts = get_posts(array('post_type' => 'post'));

    echo '<h2>Pages</h2><ul>';
    foreach ($pages as $page) {
        echo '<li><a href="' . get_permalink($page->ID) . '">' . $page->post_title . '</a></li>';
    }
    echo '</ul>';

    echo '<h2>Posts</h2><ul>';
    foreach ($posts as $post) {
        echo '<li><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a></li>';
    }
    echo '</ul>';
}

add_shortcode('html_sitemap', 'generate_html_sitemap');

  1. Use Shortcode in Page/Post:
    • Save the changes to functions.php.
    • Now, you can use the [html_sitemap] shortcode in any page or post to display the sitemap.

Keep in mind that this is a basic example, and you might need to customize it based on your specific requirements. Additionally, manually updating theme files carries the risk of causing issues if not done carefully, so make sure to have a backup before making any changes.

If you’re not comfortable with coding or want a more advanced and robust solution, using a dedicated plugin for generating and updating HTML sitemaps might be a safer and more efficient choice.

Leave a comment