Creating a Static/HTML Sitemap in WordPress Without a Plugin

Creating a Static/HTML Sitemap in WordPress Without a Plugin

We have put together a quick guide to creating a static/HTML sitemap in WordPress without having to install a plugin.

By using the built in wp_list_pages function combined with a shortcode you can quickly create a simple and lightweight static sitemap for your site.

Pre-requisite: You will need to be able to edit your functions.php (preferably in a child theme).

Yoast

If you use Yoast SEO and don’t want to include pages that have been noindexed in your static sitemap, you can make use of the code in this post from Lime Web Development.

add_filter( 'get_pages', 'lwd_exclude_no_indexes_from_wp_list_pages', 10, 2 );

function lwd_exclude_no_indexes_from_wp_list_pages( $pages, $args ) {
if( array_key_exists( 'walker', $args ) ) {
foreach ( $pages as $key => $item ) {
$_yoast_wpseo_meta_robots_noindex = get_post_meta( $item->ID, '_yoast_wpseo_meta-robots-noindex', true );
if( $_yoast_wpseo_meta_robots_noindex == 1 ) unset( $pages[$key] );
}
}
return $pages;
}

function lwd_no_plug_html_sitemap() {
$pages = wp_list_pages([
'echo' => false,
'title_li' => ''
]);
return $pages;
}
add_shortcode( 'lwd_no_plug_html_sitemap', 'lwd_no_plug_html_sitemap' );

In the above example the shortcode you would use is:

[lwd_no_plug_html_sitemap]

All in One SEO Pack

If you use All in One SEO Pack the code is slightly different, as follows:

add_filter( 'get_pages', 'lwd_exclude_no_indexes_from_wp_list_pages', 10, 2 );

function lwd_exclude_no_indexes_from_wp_list_pages( $pages, $args ) {
if( array_key_exists( 'walker', $args ) ) {
foreach ( $pages as $key => $item ) {
$_aioseop_noindex = get_post_meta( $item->ID, '_aioseop_noindex', true );
if( $_aioseop_noindex == 1 ) unset( $pages[$key] );
}
}
return $pages;
}

function lwd_no_plug_html_sitemap() {
$pages = wp_list_pages([
'echo' => false,
'title_li' => ''
]);
return $pages;
}
add_shortcode( 'lwd_no_plug_html_sitemap', 'lwd_no_plug_html_sitemap' );

Again, in the above example the shortcode you would also use:

[lwd_no_plug_html_sitemap]

You might find our post on how to create a WooCommerce product sitemap interesting.

Need help with WordPress? Search Candy provide WordPress design and development services – click here to find out more.

WordPress

Leave a Comment

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