# HTML Sitemap Shortcode

> Adds an [html_sitemap] shortcode that lists all your pages and your posts grouped by category, giving visitors and crawlers one page that links to everything.

- Type: PHP
- Runs: Run everywhere
- Category: [SEO](https://wpide.com/library/category/seo/)
- Tags: sitemap, shortcode, navigation
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/html-sitemap-shortcode/

## Code

```php
add_shortcode( 'html_sitemap', function ( $atts ) {
    $atts = shortcode_atts( array( 'show_posts' => 'yes', 'posts_per_category' => 50 ), $atts, 'html_sitemap' );
    $html = '<div class="wpide-html-sitemap"><h2>Pages</h2><ul>' . wp_list_pages( array( 'title_li' => '', 'echo' => false ) ) . '</ul>';

    if ( 'yes' === $atts['show_posts'] ) {
        foreach ( get_categories( array( 'hide_empty' => true ) ) as $category ) {
            $posts = get_posts( array(
                'numberposts'         => absint( $atts['posts_per_category'] ),
                'ignore_sticky_posts' => true,
                'no_found_rows'       => true,
                'category__in'        => array( $category->term_id ),
            ) );

            if ( ! $posts ) {
                continue;
            }

            $html .= '<h2><a href="' . esc_url( get_category_link( $category ) ) . '">' . esc_html( $category->name ) . '</a></h2><ul>';

            foreach ( $posts as $item ) {
                $html .= '<li><a href="' . esc_url( get_permalink( $item ) ) . '">' . esc_html( get_the_title( $item ) ) . '</a></li>';
            }

            $html .= '</ul>';
        }
    }

    return $html . '</div>';
} );
```

## Notes

Use [html_sitemap posts_per_category="20"] to limit long categories, or [html_sitemap show_posts="no"] to list pages only.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "HTML Sitemap Shortcode" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
