Adding a Sitemap to a Wordpress Blog
One thing I’ve never liked about blogs is the failure to include a single list of all posts by title. The archive function returns full results, and you have to click through page after page to see what is there. If the titles are informative, they provide clue enough to what the post is about, why not have a simple list of all posts?
I looked briefly at existing Wordpress plugins to see if someone had something simple, but they tended toward XHTML sitemaps that were Google compliant, or fancy versions of the existing archive function. I just want a simple linked list of all titles, those were over-kill. Rather than waste even more time searching for something, I just wrote a simple script. Here is the main file, which I call as a php include:
Just change the $ptitle variable to begin with whatever prefix your blog uses, and call this with an include from your theme. Just how you call it is difficult to explain, because it changes from one theme to the next. First, decide where you are going to put it. On some blogs I created a new page and called it ‘Site Index’ or something similar, and called the sitemap when that page was displayed. On this site I have just added it to the existing ‘about’ page — the link is at the upper right corner of every page. To have the sitemap code included only on that page, I added an if() clause to theme. Some themes have a ‘page’ file, and it goes there. Others don’t have that and it needs to go in the ‘index’ file. No doubt there are other themes that use other files, you may need to experiment. Then, in the theme file, find which part of the code displays the content, and replace it with your loop. Suppose you saved the sitemap file as map.php and the page you want to include it on is called ‘Sitemap’ — you include something like this: A sitemap will ensure that every page on your blog gets indexed by the search engines, and also provides a convenient way for new readers to become familiar with your content.<ul>
<?php
$ptable='wp_posts';
$sql="select post_title,guid from $ptable where post_type='post' and post_status='publish'";
$res=@mysql_query($sql);
if ($res)
{
if (mysql_num_rows($res))
{
while ($row=@mysql_fetch_array($res))
{
$ptitle=$row['post_title'];
$purl=$row['guid'];
echo("<li><a href=\"$purl\">$ptitle</a>\n");
}
}
}
?>
</ul>
if(is_page('Sitemap')){ include('map.php');}else{ [existing content code] }
No Comments
You must be logged in to post a comment.