How to: Display sitemap page in WordPress with shortcode


I wanted a way to quickly display all the pages within a WordPress site easily and effectively using the least amount of code.

By adding the following code into your themes functions.php file and simply adding nothing but the shortcode [sitemap] into the page content, you’ll have all your pages shown in correct hierarchy on a single page which is a great additional navigation page that I know a lot of people use.

<?PHP

add_shortcode('sitemap', 'wp_sitemap_page');

function wp_sitemap_page(){
	return "<ul>".wp_list_pages('title_li=&echo=0')."</ul>";
}

?>

By utilising the built-in WordPress function wp_list_pages, you can quickly show a list of all the pages within your site.