Call Chris at (615) 521-1890 for a free quote.
615.521.1890
Nashville Interactive - Home

Blog

WordPress Sitemap Page Without a Plugin

Sep 13 2017

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.

/////////////////////////////////////
/* DIY Sitemap without a plugin... */
/////////////////////////////////////</code>

// Front-end Shortcode Usage: [html_sitemap exclude="123,456,789"]

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 .= '<ul class="sitemap_list">';
 
  foreach ($pages as $page) {
    $return .= '<li><a href="' . get_page_link( $page->ID ) . '">' . $page->post_title . '</a></li>';
  }
 
  $return .= '</ul>';
 
  return $return;
}
add_shortcode( 'html_sitemap', 'get_html_sitemap' );