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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.