Software, Technology

How-to: WordPress Sitemap Page Without Plugins


Geographica restituta per globi trientes - Norman B. Leventhal Map Center at the BPL via Wikimedia CommonsHaving 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 incompatible 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

Related: How-to: Understand WordPress’ Goshdarnit

​​​​​Image credit: Geographica restituta per globi trientes – Norman B. Leventhal Map Center at the BPL via Wikimedia Commons. https://lh5.googleusercontent.com/-v3aZCt-HbyQ/U8cOcbyo2oI/AAAAAAAAB2I/9YFucfA2UUA/s764/Geographica_restituta_per_globi_trientes_NormanBLeventhal_crop.jpg

About Robin Catling

Writer; performer; project manager; sports coach; all-round eccentric.

Discussion

One thought on “How-to: WordPress Sitemap Page Without Plugins

  1. Im using Google sitemap generator 4.0.7 on http://www.brightverge.com, search this plugin on wordpress.org and install with easy steps all settings are default. i strongly recommend this wp plugin.

    Posted by Bright Verge | October 11, 2014, 8:00 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Twitter Updates

Follow us on Twitter @EverythingExpre

Find Us on Facebook

Enter your email address to follow this blog and receive notifications of new posts by email.

Categories

Library

BBC World News

BBC World News
Opens the BBC World News page.
%d bloggers like this: