Custom WordPress pages for sitemap support

As tempting as rolling my own blog engine was, I opted to use wordpress for the time being. I may revisit that decision in the future, but getting something up and running seemed more important than wasting time on a side project. WordPress integration has added a new set of tasks.

This website uses scripts to automatically generate sitemaps, about 8.5 megs worth of uncompressed sitemap xml. Adding the wordpress based entries to those sitemaps could be done in a handful of ways. Running SQL commands through the command line RDBMS interface, manually updating the scripts, etc. I chose to generate sitemap type pages using wordpress itself, providing a raw spider friendly link page.

Adding a new wordpress page starts in the theme directory. Simply create a new .php file with the template name specified in the header and your custom body. The wordpress imports are automatically provided, so simple start calling functions. Here is how I generated a page with links to each post, using the post title as the link text.

<HTML><BODY><UL ID="post_links"> <? /* Template Name: List post links */ $postID = $wpdb->get_col(" SELECT ID FROM $wpdb->posts WHERE (post_type = 'post') AND (post_status = 'publish') AND (post_password = '') "); foreach($postID as $post_link) {     echo "<LI><A HREF=\"".get_permalink($post_link)."\">".get_the_title($post_link)."</A></LI>"; } ?> <UL></BODY></HTML>

The above code is placed in a file named “all.php” in my themes directory. Now you can add a new page in the wordpress dashboard by clicking “Pages->Add New” and set the template as “List post links”. Note that the “Template Name:” line in the php file is used in the template dropdown selector.

With giving me a list of all public blog entries, the sitemap generation scripts can retrieve the list using “fetch -o –” with the appropriate sed-fu to reformat into the appropriate XML.

Last modified on September 8, 2012. This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.