HTML Sitemap in WordPress Child Theme

html-sitemap-pagesonly

 

Google started using sitemaps in 2005. Shortly after, in 2006 an industry standard schema was established with other search engines. Generally speaking, sitemaps are intended for search engines. These types of sitemaps are xml sitemaps and every website should have one for the search engines. HTML sitemaps on the other hand are like content archives and are more for the user but also beneficial for the search engines. They are actual webpages that include links to all of the pages in your site and can be organized by author, category, publication date, etc.

HTML sitemaps display links to all of your published content. If some of your content becomes archived quickly and is no longer linked to by the active pages on the site, an XML Sitemap will supply the URL links to be crawled. Here is where the HTML sitemap comes in. By also using an HTML sitemap, you provide at least one link in the active site to each published page. This ensures that people who come back to your site can find their favorite posts and that search engines can recognize your older pages as valuable enough that they are linked to active pages.

Let’s get started and create an HTML Sitemap in WordPress Child Theme

First create a folder in your child theme called partials. Then create a file inside that folder called sitemap.php Add the following code to sitemap.php. This code is lays out how you want your sitemap information to be displayed:

<h2 id="posts">Posts</h2>
<ul>
<?php
// Add categories you'd like to exclude in the exclude here
$cats = get_categories('exclude=');
foreach ($cats as $cat) {
  echo "<li><h3>".$cat->cat_name."</h3>";
  echo "</ul><ul>";
  query_posts('posts_per_page=-1&cat='.$cat->cat_ID);
  while(have_posts()) {
    the_post();
    $category = get_the_category();
    // Only display a post link once, even if it's in multiple categories
    if ($category[0]->cat_ID == $cat->cat_ID) {
      echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    }
  }
  echo "</ul>";
  echo "";
}
?>

<h2 id="pages">Pages</h2>
<ul>
<?php
// Add pages you'd like to exclude in the exclude here
wp_list_pages(
  array(
    'exclude' => '',
    'title_li' => '',
  )
);
?>
</ul>

Then create a template by copying the page.php template. Replace everything about the content with this code:

<?php get_template_part('/partials/sitemap'); ?>