A sitemap page is a staple and an SEO essential for any site with a good amount of varied content. There are a lot of plugins out there that come with pages and pages of individual options customisations, usually missing the one or two things you actually want, and overcomplicating the whole process.

It is very simple to build a basic sitemap page using a couple of basic queries and loops that are probably already used throughout the rest of the site. This example involves creating a page in the admin called Sitemap and adding the following code to a file called page-sitemap.php. Adapting the example for your own site is as simple as changing the names of the post types and adding a function to return all posts of that type.

In the example, all the site sections that need to be included in the sitemap are displayed. These are pages, clients and blog articles. Pages and blog articles are defaults, and clients is a custom post type.

Each section has several lines that are nothing new or special within WordPress.

 

$all_pages = get_all_pages();

Will run a function called get_all_pages that is defined in functions.php, and simply runs a query to return all of the pages on the site. All of the other sections run similar functions that return the posts of that type. It is likely that these functions already exist if the rest of the sites development is nearly finished. The ones used are shown at the bottom of this page.

 

if ( $all_pages->have_posts() ) : while ( $all_pages->have_posts() ) : $all_pages->the_post();

This is a very typical WordPress loop that checks for posts, loops through them and sets the active loop post to the next one in the while loop.

 

<a href=<?php the_permalink(); ?>><?php the_title(); ?></a>

This part will show the title and create a link to the page / post / custom post type.