Technical Blog

In-Blog Tag Viewing

Posted on

This hack has been superceeded by a newer version.

A recent post at Freshblog got me to thinking about integrating the viewing of tags directly into my blogger blog. The solution presented there loads the results from tag queries into your sidebar, and checks the referrer if no tags are specified. The idea here is that if you are on my blog and query a tag, and then click a link to his blog, his sidebar will show his posts for the tag you selected here. It’s a great system, but my only problem with it was that it reloaded the current page with no apparent change at first, until you checked the sidebar. So I have modified the code for my own hack.

This hack will present a drop-down list of tags in your sidebar, pulled from the account you use to tag your blog and filtered for the anchor tag you use (if you use one). Selecting a tag from the drop-down list will take you to a post page on your blogger blog, so you never leave the site, that lists the results of a tag query for the selected tag. The sidebar will also update the same was as on Freshblog to facilitate the inter-blog browsing. Selecting another tag from the drop-down in the sidebar will add that tag to your query, not replace your query.

A ‘reset’ link is placed next to the drop-down box so that you can clear the tag query and switch to another tag without adding it. My version also fixes a minor bug in the original so that when tags in a referrer URL have ‘%20’s in them they are replaced by the ‘+’

If no tag is found in the <head> section, the script attempts to pull one out of your posts. This can be disabled on your main page by inserting this code along with that in step 2:
<MainPage><script type=”text/javascript”>var no_autocapture = true;</script></MainPage>

Now, without further boring explanation, here’s how to implement the hack :

  1. Create a new post on the blog you would like to implement this hack on with the title ‘Tags’ and click the ‘Edit Html’ tab for the post body, pasting the exact code ‘<div id=”tagspostpage”></div>’ into the post body. Turn comments off for the post and set the post date to January 1, 2000. The time doesn’t matter. Post this post.
  2. Edit the template for your blog and insert the following code into the <head> section :

    <script type=”text/javascript”>
    var del_user = “username“;
    var anchor = “anchortag“;
    document.write(‘<script src=” type=”text/javascript”><\/script>’);
    <script src=” type=”text/javascript”></script>

    Replacing ‘username’ with the username where you tag your blog posts and ‘anchortag’ with the anchor tag you use (if you use one). If, like me, you don’t use an anchor tag, then replace ‘anchortag’ with nothing.

  3. At the bottom of your template, right before the ‘</body>’ tag insert this code :

    <script type=”text/javascript”>
    var thediv = document.getElementById(“tagspostpage”);
    thediv.innerHTML = return_tagged_posts(true);

  4. Insert this code into your template where you want the tags drop-down box and matched posts to appear (as they do in my sidebar) :

    <script type=”text/javascript”>

  5. Save your template and republish your whole blog.

7 Responses

Johan Sundström

Nice; now I won’t have to play much myself to try out the looks with some excerpts. 🙂

A small additional suggestion: in your template, add an id or class attribute to the body tag, passing the post id. Then you can hide the silly “Posted at …, permalink, Trackback(0)” div with some CSS (body#4711 { display:none; } or similar).


Good work, Stephen!

Definitely a step forwards. Please see my comment at Freshblog for further throughts.





Excellent!!! Neater, cleaner, and still passes the tag. Very cool.


Neat hack, though I personally would prefer to be able to *change* categories more easily, so that when I click a new tag, it replaces rather than appends to the current tag. Is there any easy way I could change the code to achieve this?


Yes, changing to a new tag instead of adding is a minor glitch in the way this works. I’ve been thinking about adding a link next to the drop down box that clears the current tag query. Unless someone has a better idea?

Leave a Response