Tuesday, 15 July 2014

How-to: WordPress Sitemap Page Without Plugins


Having inherited development of a client's site, I looked at the amount of clutter my illustrious predecessor had built up and decided it was time for a spring clean. It ddin't help that some of the old plugins proved imcompatible by the time we upgraded Wordpress through six or seven versions.

I've come across recommendations for WordPress sites to function with a maximum of 10 plugins, for reasons of speed, flexibility, security and compatibility. Since we already had a site that was slow-loading, throwing errors and bulked up with plugins I hadn't a clue how to work, it seemed like a good idea.

Then I realised the site needed a site map...


Could I create a simple WordPress Sitemap Page without using plugins to includes Pages, Posts and Custom Post Types? Yes.

Could I create one that actually worked with the theme I'm using? On my second attempt, yes.

Needless to say you need to have administrative access to your WordPress site and FTP access.

1:In your active theme folder, create a file named sitemap.php.
2: Paste the following code in to the sitemap.php file.

<?php
/*
Template Name: Sitemap
*/
?>
<?php get_header(); ?>

<div id="content" style="float:left;">
  <div class="post">
    <div>
      <h1 style="font-size:x-large; font-weight:bold;">Sitemap</h1>
    </div>
    <div class="entry siteMap">&nbsp;

     <h2 id="posts">Posts</h2>
      <ul>
        <?php
// Add categories seprated with comma (,) you'd like to hide to display on sitemap
$cats = get_categories('exclude=');
foreach ($cats as $cat) {
  echo "<li><h3>".$cat->cat_name."</h3>";
  echo "<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 "</li>";
}
?>
      </ul>
<!-- ----------------- Display Categories ----------------- -->
      <h2>Categories</h2>
      <ul>
        <?php wp_list_cats("sort_column=name&feed_image=/wp-content/themes/Horcrux/images/rss-small.png&optioncount=1&hierarchical=0"); ?>
      </ul>
<!-- ----------------- Display Pages ----------------- -->
      <h2 id="pages">Pages</h2>
      <ul>
        <?php
// Add pages seprated with comma[,] that you'd like to hide to display on sitemap
wp_list_pages(
  array(
    'exclude' => '',
    'title_li' => '',
  )
);
?>
      </ul>
    </div>
  </div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>


Note that for this sitemap template to render properly with my theme, I have to float the main content <div> (else my sidebar drops below the foot of the sitemap) and do a bit of styling of my main heading text.

6. Create a new page in the backend of your WordPress install. Select the page template – HTML Sitemap.

This bit of PHP code scans your Wordpress database and pulls in all the post,s categories and pages you have published on your site. You may need to have another spring-clean to tidy up and organise all those elements into a coherent page for your site visitors. RC
​​​​​
Image credit: Geographica restituta per globi trientes - Norman B. Leventhal Map Center at the BPL via Wikimedia Commons.

1 comment:

  1. I've used similar approach that allows me to write the content through the visual editor as well. There is also a way to create a HTML sitemap without the plugin and separate sitemap page template. Check it out http://learnedia.com/html-sitemap-wordpress-without-plugin/

    ReplyDelete

At least try to be nice, it won't kill you...