Adding categories to Blogger blogs

27th September 2005 update

I’m trialling a new piece of software called Caterblogger. It will add categories to any Blogger blog. You can register interest in it on the site.

The original post follows below:

Blogger is really ease to use and set up, and manages individual posts really well. It’s also useful to be able to send posts in by e-mail, and there are quite a few different ways of configuring how posts are displayed. With some HTML knowledge, the template can be made to look exactly how you might want it. It also manages things like archiving particularly well, and the way posts are published with filenames matching the page title (mostly) is really good for being found in the search engines. What it doesn’t do, though, which would be really useful, is to allow posts to be put into categories. I was keen on implementing it on this blog, however, and thought that with a bit of code I could work it out. So I did, and the results can be seen on the right hand side of the page under the ‘Categories’ heading.

It’s not exactly straightforward, though. To get it working using the method I’ve used you’ll need the following:

  • A blogger account (duh)
  • Your own hosting that supports ASP.Net pages, where you can also change file permissions to allow IIS/ASP.Net to write to the file system
  • To get it working without recompiling, the pages will need to be served from the web root (since I haven’t bothered adding anything to web.config, which I really should do…)
  • Some knowledge of ASP.Net and preferable C#
  • The ability to edit your own blog’s HTML templates
  • Not be afraid of embedding XML in your posts, and troubleshooting errors
  • To customise the category layout: knowledge of CSS and, preferably, XSL

So, it’s not for the unprepared. I mean it – without a fairly good idea of what you’re doing, you’ll probably end up doing more harm than good. (I’d recommend installing it all locally first either way.) I’m also unlikely to be much help if something doesn’t work, so this is all at your own risk.

The principle behind my Blogger categories

I’ve written the code to make it as simple to put posts into categories as possible once the template has been set up. A post is put into a category by placing an XML tag into the posting text. The format of these categories is

Once a new post is published it is posted to the homepage of your blog by Blogger. My code registers that the blog content has changed and parses through all of the postings in the various subdirectories (by looking at files in subdirectories ending in .aspx and not containing the word ‘archive’ in the filename). It looks for the

tags and pulls out all of the category names that are contained in it and builds a new XML document containing a list of all of the categories and all of the files that are within each category. Each post can be in multiple categories.

The advantage of this is that categories don’t need to be set up beforehand – as soon as a post is published in a category, that category is created. The downside is that you need to make sure you spell the categories consistently.

Setting up Blogger categories

The files that are needed are in the zip file which you can download here. You’ll need to make sure that either IIS or the ASP.Net process has write permissions to the root of the blog, depending on the user that IIS is being. If you get a file permission error and change web.config to show you what’s going on then you’ll see which one you need to set up. Otherwise, as these files are, if deployed correctly onto a new web application at root level they should work straight out of the zip. However, you’ll get my blog content (as captured at a point in time since index.aspx is not going to update) with links to my images. If you get a compilation error at this stage then fix it now before you go any further. (Possible errors would include file write permissions or path problems. Anything else may be more serious.)

Once the code runs without your blog being connected to it, what you need to do to get it working is:

  1. Add code to your own Blogger template
  2. Update the HTML of postsByCategory.aspx to suit your own blog
  3. Change your Blogger settings to publish all files out as .aspx
  4. Add category XML to your posts
  5. Republish each post of your blog
  6. Check for XML errors and tidy up
  7. Update the category listing XSL files to match the look and feel of your blog.

1. Add code to your own Blogger template

Firstly, the page needs to be made a true aspx page. Add

to the top of the page.

Next, the category list and other information needs to be generated within each post. A small piece of code needs to be added to your posting body. This looks like the following:

" />
" />

And is wrapped around your existing tag.

What this does is wraps the whole post in an XML block (this will create invalid XHTML, but Blogger does that anyway most of the time) and add some extra information such as the title and the date of the post as XML nodes containing Blogger tags.

You’ll also need to add an asp:Literal to your template to display the categories list. This is a simple tag which can be added wherever in the HTML you want the category list to appear:

2. Update the HTML of postsByCategory.aspx to suit your own blog

postsByCategory.aspx is a standalone file so updating it should be an easy HTML job. You shouldn’t need to change any .Net code

3. Change your Blogger settings to publish all files out as .aspx

See my previous post Publishing with aspx extension from Blogger.

You’ll also need to change the post timestamp format to English date, e.g. 27.07.05 (for 27th July 2005), as this is used for ordering posts (in reverse date order) in the category list. This is found in the Blogger console -> Settings -> Formatting as Timestamp Format.

4. Add category XML to your posts

As mentioned earlier in this post, to put a post in a category you just need to type

(where ‘Category name’ is the name of the category you want to appear) in your post. This can be anywhere within the text of the post. I tend to put it at the beginning so it’s easier to find, but it doesn’t really make any difference.

5. Republish each post of your blog

This is a pain but it’s the only way to get each file published as an aspx file. You’ll need to do it if you’re putting your posts in categories in any case. Each post will need to be republished individually – you can’t do a ‘Republish all’.

6. Check for XML errors and tidy up.

Once you view any page on your blog, a file called categories.xml will be published in the web root. Download this and open it up in a text editor or any other viewer. As well as category nodes, there are likely to be a number of nodes – these show which files contain invalid XML in the posts so you should take a look at them and try and fix the error. The code has been written to skip any errors so you should get a category list generated as long as at least one post is valid. The file should be generated regardless since it also contains error nodes. Errors are often caused by invalid characters in post bodies (e.g. pound symbols not being inserted as character codes) or, slightly more annoyingly, double quotes in post titles (since the post title is contained in an attribute node in the template in our little XML block).

7. Update the category listing XSL files to match the look and feel of your blog.

This should be fairly straightforward even if you don’t know XSL well, just don’t break the templates. The HTML is very simple and you may even be able to get away with just changing the CSS instead.

That should be it

In theory, that should all work. In practice, I’m willing to be about 75 things have gone wrong so far and now you wish you’d never started. For the really keen (or more likely for bug-fixing) you can download all the source files here (and let me know if anything’s missing). Best of luck!

Download the Blogger categories compiled files
Download the Blogger categories source files

4 thoughts on “Adding categories to Blogger blogs”

Leave a Reply

Your email address will not be published. Required fields are marked *