Websites, Mashups and more

Monday, January 16, 2006

Code Snippets: Categorizing Blogger entries

Update 02/08/06: This posting explains how to create categories using the posting's title. If you don't like inserting the category name in the title you can read my other posting (Blogger Categories in 5 minutes)

---------

I was extremely disappointed when I found out that blogger.com does not have a way to categorize your posts. I started looking on the Blogger Addins section which recommended doing a search on Google. I am not sure why categorization for postings is not part of the blogger package. As a programmer I am almost positive that categorization does not require any special coding logic.

Anyway, I started looking for possible plugins. My initial search lead me to few interesting posts however, I was not satisfied with any of them. To mention a couple:

1) OldCola: His method is to create a blog for each category. The user would then create the posting in the desired category, which would send an email to some email account. The email account would then forward the message to the main blog. The problem with his approach is that you will be creating two entries for each posting. Plus you will have to manage multiple blogs rather than one.

2) There were also other methods that involved using delicious. I did not like this approach

As usual I decided to find my own solution through coding. It was actually very simple. The blogger.com provides a search engine for blogs. All I needed to do is use the search engine and pass it the query that I am interested in. I started by looking at the advanced search options for the search.blogger.com. I found out that you could perform a search for a specific phrase in the posting's title. You can also limit the search to a specific blog. That’s basically all I needed.

I started by using search.blogger.com to search for a specific title in my blog. The search.blogger.com generated the following URL in the address bar:

http://search.blogger.com/?Submit=Search+Blogs&as_lq=
ui=blg&hl=enas_q=&as_epq=as_oq=&as_eq=
bl_pt=%22Virtual+NYC+Tour%22&bl_auth=as_drrb=q
&as_qdr=as_mind=1&as_minm=3&as_miny=2005
&as_maxd=16&as_maxm=1&as_maxy=2006bl_bt=
&bl_url=websitesandmore.blogspot.comlang=all
&safe=active

The URL is very long but has a lot of unnecessary text. I trimmed the URL down to the essentials, which made it look like the following:

http://search.blogger.com/?ui=blg&bl_pt=Virtual+NYC+Tour
&bl_url=websitesandmore.blogspot.com&as_q=

I then created a javascript function that will be responsible for showing the category. Insert the following code in any part of your blog template after the <html> tag:


<script language="javascript">
var blogUrl = "YOUR BLOG ADDRESS";
function showCategory(category){
var encodedCategory = escape("\"" + category + "\"");

var url = "http://search.blogger.com/?ui=blg&as_q=";
url = url + "&bl_url=" + blogUrl + "&bl_pt=" +
encodedCategory + ":";
window.location.href = url;
}
</script>



Make sure to replace the above words [YOUR BLOG ADDRESS] with your blog's address. Don't include the www part of the address. inclose the blog's address in qutations

Categorizing your posts
Every time you create a new posting make sure to prefix the title of the posting with the name of the category. That’s all. For example, if your posting has the title "Nice Flowers" and you want to add it to the category "Garden" then your posting's title should look like this

Garden: Nice Flowers

Creating the Category URL in the sidebar
To create a category section for your blog. The URL for a category should look something like this


<a href="javascript:showCategory('THE CATEGORY');">THE CATEGORY</a>

Clicking on the link will display all the postings under that category in the blogger search page.

I did not intend this at the begining but I realized that you can even add your posting to more than one category. All you need to do is prefix all the categories to the posting's title. You can seperate categories with commas or semi cololns. For example: If you want to add the "Nice Flowers" posting to the categories "Garden" and "Environment" then the title should look something like this

Garden, Environment: Nice Flowers


The only problem with this solution is that if you insert a new posting it takes few minutes until the blogger search engine indexes your page. So, in the first few minutes clicking the category link won't display your latest posting. I have tested this and I found out it takes around 5 - 10 minutes for your result to be displayed in the category search page. Not bad

You can test the categories in my blog

Categories: Code Snippets_, Blogger Categories_

35 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home