Monthly Archives: May 2004

On Asides

As you may have noticed on this page or in your aggregator my normal entries are now interspersed with smaller link and commentary entries represented in an unordered list. These fall in chronological order with my other entries and are real posts with permalinks, comments, categories, trackbacks, and pingbacks. I have been wanting to do this for a long time and there was a flood of entries when I first got this working. I fully expect to post in this category with a much higher frequency than my normal posts. I come across things all the time that I want to link so badly but I just don’t have the time to write an entry about. Now every interesting tidbit I come across is just a click of a favelet away from my readers. It’s liberating.

The format of a weblog dictates its writing. There is no getting around this. Ever since my redesign I’ve had these big important titles that—as a writer—are intimidating. Everything I write has to be worthy of its 32-point Dante banner. This was a deliberate to force myself to put more thought and effort into my entries and it has worked; some of my best writing has been since the redesign. It has been stifling as well. To express what I want to express these days I need something more dynamic.

This type of linklog is nothing new. I first saw it at Kottke and since have enjoyed it at Charles Gagalac’s site. Charles was the first person I know of to do it in WordPress. This inline style fits my requirements very well, the biggest being that I wanted to use my existing post taxonomy so a second weblog would have been inconvienent. I also get the benefit of a combined RSS feed and combined update pinging. More than all of that though, I don’t want to limit myself to one type of post in a new weblog, or in this one. I already have plans to use this feature to not only highlight interesting links, but comments I make on other sites, upcoming gigs I have, photolog updates, and various activities and writing I now have spread out over a half-dozen blogs. It’s a perfect application for WordPress‘ multiple sub-categories feature. Technically, the whole thing was very easy to implement.

First I made a new category all of these posts would be a part of, it was late and I couldn’t think of anything too clever, so I went with “Asides.” This may have been subconciously inspired by Movable Blog. So I created the category in WordPress, and it had an ID of 33. Next it was simply a matter of telling my template that if a post was in the Asides category to lay it out a little differently. I wrote a quick function, in_category() that does this with no additional DB queries that is now in the WordPress CVS and will be in the 1.2 release. The function takes a single argument, a category ID, and it returns true if the current post is in that category. Here’s what I have in my post loop:

<?php if (in_category(33) && !$single) { ?>
<ul class="asides">
<li id="p<?php the_ID(); ?>"><?php echo wptexturize($post->post_content); echo ' '; comments_popup_link('(0)', '(1)', '(%)')?> <?php edit_post_link('(e)'); ?></li>
</ul>
<?php } else { ?>

So basically what this is saying is that if the post is in category #33 and we’re not on a permalink page to format it using that code, otherwise do our normal thing. We have a simple unordered list with a uniquely ID’d list item. (The ID is a throwback to about two years ago when I had a heavily hacked version of b2 with monthly archives. Can’t let those permalinks break.) I get the post content directly because I didn’t want to mess with the normal filters (like autop) applied to the_content(). I run it through texturize to keep everything pretty. Then I have a simple comment link, and finally a link that only I see that allows me to easily edit any entry. This worked great, but you can see that I have a separate unordered list for each entry, which isn’t what I want; I want all consectutive aside entries to be part of a single list. This gets tricky because you have to start the list when the current entry is an aside but the newer entry and stop it when the next entry is not an aside. Tricky tricky, and I don’t want to think what would happen if someone decided to reorder my content. I had only allocated myself ten minutes for this entire project and I was at minute number eight, so it came time for ugly code:

function stupid_hack($str) {
return preg_replace('|</ul>\s*<ul class="asides">|', '', $str);
}
ob_start('stupid_hack');

I swear that’s the actual code in my template. Put it somewhere around the top of the template, it really doesn’t matter where. It’s a hack, but it’s been working great. Simon says it is “ugly as sin,” and that’s what I thought when I wrote it. Since then, however, it has grown on me a bit. It’s like the supermodel who goes out every day dressed to the nines relaxing at home in a bathrobe, slippers, and no makeup. I keep the pretty code elsewhere, here at home I’m a little more casual. Perhaps when I have a little free time I’ll put in the more complicated logic I described above but if it ain’t broke…