WordPress Sitemap Page Without a Plugin

Plugins are great sometimes. Other times, it’s much better to just drop a few lines of code into your theme’s functions.php file to simplify things and help with performance. Here’s a relatively simple DIY WordPress sitemap complete with shortcode that’s easily dropped into any WP page. You can use the ‘exclude’ parameter to exclude specific pages by ID.

[cc]
/////////////////////////////////////
/* DIY Sitemap without a plugin… */
/////////////////////////////////////

// Front-end Shortcode Usage:

function get_html_sitemap( $atts ){

extract( shortcode_atts( array(
‘exclude’ => ”
), $atts, ‘sitemap’ ) );

// arg reference here. add to array below as-needed: https://developer.wordpress.org/reference/functions/wp_list_pages/
$pages = get_pages(array(
‘title_li’ => ”,
‘post_status’ => ‘publish’,
‘exclude’ => $exclude,
));

$return .= ‘

    ‘;

    foreach ($pages as $page) {
    $return .= ‘