May
19

On Asides

Filed under: Meta | May 19th, 2004
Post

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…

197 Responses

Alex | May 19th, 2004 @ 4:07 am

Thank you for sharing the information, I had “asides” running on my site for some time now using this tip. I tried to impliment your method (because I does not mess with autop), but it creates a weird problem with links. s are added where they don’t belong in links and abbreviations i.e. <a href="http://secure.sixapart.com/">?

nikkiana | May 19th, 2004 @ 4:48 am

/me decides she needs to do this with the next generation of everytomorrow.org.

Alex | May 19th, 2004 @ 5:32 am

Argg, my comment got all messed up.

Michael Heilemann | May 19th, 2004 @ 6:27 am

I’ve been using Charles’s variant for a few weeks now, and it’s working great. Except of course for the fact that entries are being sorted chronologically and not categorically and then chronologically. I swear, I need to teach myself PHP so I can do these kinds of things myself and not continually apply pressure to you WP guys ;)

Geof | May 19th, 2004 @ 8:55 am

An interesting approach.

Beth | May 19th, 2004 @ 9:34 am

I’d really like to get this code up and running on my blog but I’m having trouble. I’m struggling to know where in the loop to place the code and how to terminate it so that I don’t get a parse error at this line
<?php endforeach; else: ?>

Have you any advice? Thanks

Trent | May 19th, 2004 @ 1:53 pm

Ever since my redesign I’ve had these big important titles that—as a writer—are intimidating.

Oh my god, I wrote almost the same exact sentence yesterday in my post announcing my redesign. The problem with the way my old site used to look was each post felt so BIG. Hopefully, my new design will make it easier on the eyes when I post a bunch of one or two liners.

John | May 19th, 2004 @ 3:23 pm

Matt, can you provide where you added the < ?php if (in_category(33) && !$single) { ?>… code?

Pingback: Virulent Memes

David Collantes | May 20th, 2004 @ 6:55 am

Matt, you are mssing a on the stupid_hack, it is:

return preg_replace('|s*

    |’, ”, $str);

And it should be:

return preg_replace('|s*

    |’, ”, $str);

David Collantes | May 20th, 2004 @ 6:56 am

Darn, comments destroyed. It should be s* instead is just *s.

Matt | May 20th, 2004 @ 7:06 am

ENCODE YOUR HTML! Or don’t post code.

Pingback: engadgeted.net

Chris Owens | May 22nd, 2004 @ 2:06 pm

RE: Comment 6

I have the same problem, where exactly are we supposed to put the code if we were using the default unmodified template?

Mark | May 22nd, 2004 @ 11:41 pm

You preg_replace is a little borked :)

`return preg_replace(’|s*

    |’, ”, $str);`

flump | May 24th, 2004 @ 9:20 am

it seems i have to scroll down a page before i can get to your content. i don’t really know what all those links are, and wether or not i should be reading them.

*shrugs*

Pingback: atog » Asides

Chris L | May 25th, 2004 @ 12:42 am

I’ve been thinking about how to integrate the “link and run” part of my weblog for a while. Nothing has worked as well as I would like.

But I have to say that the way you are doing it has taught me a clear lesson in how not to do it. It’s very annoying to have to sift through such a length of links to see the real content. I much prefer either a separate log (for frequent posting) or these links consolidated into a daily/weekly/irregular posting (for less frequent posting).

I hope I can find some way to subscribe to the two separately still!

Matt Brubeck | May 25th, 2004 @ 2:17 pm

Could I request a separate RSS or Atom feed that includes only the long posts (not the asides)?

Matt | May 26th, 2004 @ 2:40 am

Yeah I’m going to do a seperate feed.

Brad Froehle | May 27th, 2004 @ 6:53 pm

We know that Matt really meant to write: (note the additional \)

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

uberwald | June 4th, 2004 @ 12:00 pm

Can you give me any kind of guidance about where to place the stupid_hack code?

Matt | June 4th, 2004 @ 12:05 pm

For those having trouble I posted my entire index template to the wiki:

http://wiki.wordpress.org/PhotoMatt/index.php

Pingback: Uberwald

Rayne | June 10th, 2004 @ 10:48 am

Does that function in_category also scan the children categories? I need to exclude posts from a certain category and all it’s children, because those posts are being used for something other than my “blog”.

joshuatompkins.us - version 7.0 | June 14th, 2004 @ 7:52 pm

[...] [...]

Retrospecticus :: On the Web | June 20th, 2004 @ 8:00 pm

[...] [...]

techfreak.net » Asides | June 27th, 2004 @ 7:31 pm

[...] [...]

reizbombardement.de » Quicklinks [05/07/04] | July 5th, 2004 @ 8:55 am

[...] [...]

emmott.net » Photo Matt - On Asides | July 7th, 2004 @ 8:39 pm

[...] [...]

mark | July 8th, 2004 @ 1:30 pm

Here is my situation. I have a site where there are 5 bloggers, each with their own category. I have them all as the same level, but don’t want them to be able to edit each others posts. Is there a quick index.php hack to make this happen? I’ve posted in the wp support forum, but havent gotten an answer. Thanks.

not sweet words » On Asides | July 9th, 2004 @ 4:31 am

[...] [...]

bruce | July 11th, 2004 @ 12:49 am

Great tip.
It wouldn’t seem like such a silly hack if you called it combineAdjacentAsideLists() or something sensible like that!

Pingback: Memoirs of a Geek

Some Content » Design notes | July 15th, 2004 @ 2:48 pm

[...] [...]

Liam Page | July 17th, 2004 @ 6:39 am

Very nifty. Can’t get that stupid_hack to work for the life of me, though (PHP and WP are both still a little new to me).

Le Blaugue à Beleg | | July 19th, 2004 @ 1:01 am

[...] [...]

without loss of generality » Asides Implemented | July 21st, 2004 @ 5:51 am

[...] [...]

Nate Ritter: Web Chef » LinkLog a.k.a. Matt’s Asides | July 28th, 2004 @ 9:27 pm

[...] [...]

dr Dave | August 13th, 2004 @ 12:01 pm

Is there any reason why you would not want to use a simple flag to signal the opening and closing of a group of asides?
Granted this isn’t the prettiest code either, but it definitely beats having to parse all outputs searching for regex matches.
Something along the line of:

if (! $opened_list)
{
    $opened_list = true;
    echo "

    “;
    }

and

    if ($opened_list)
    {
        echo "“;
        $opened_list = false;
    }

both judiciously placed within the template (there’s more to it, but it’s fairly trivial, I think), ought to do the trick…

dr Dave | August 13th, 2004 @ 12:03 pm

arg. damn formatting function ate my <ul> and </ul> in the previous comment…

Pingback: nico :: pinjons >>

Matthias Heil | August 21st, 2004 @ 9:32 am

I’m having problems with this lovely hack. I get all sorts of parse errors (”unexpected $ in /…/wordpress/index.php on line 184 - that’s towards the end of my index.php (for a readable version: http://www.matthiasheil.de/wordpress/index.phps - Could it be I’m missing out on sth important here?

Thanks for your help!

Matthias Heil | August 21st, 2004 @ 4:27 pm

I’ve got it sorted - thanks to Beel@Wordpress! - I love this mod, especially when combined with the FeedDemon2WP-Hack!-)

Pingback: Binary Thoughts

Gianko | October 17th, 2004 @ 2:23 pm

I’m looking for something jut like this…

but what if I don’t want the post to appear at all!!!??…

like, I don’t want the post on the category 33 to show in my index.. never… (not even in the montly archives)

who do I do that???

can you help me???

Steven | October 25th, 2004 @ 4:25 pm

Anyone implement this with Kubrick 1.2.6?

Pingback: The Wyvern's Lair

Pingback: askewed » Asides

Pingback: Firman Pribadi

Pingback: 15june.com

Nat | July 15th, 2005 @ 2:10 pm

very cool. ive found it easier to use the miniblog plugin though

Seth | September 14th, 2005 @ 7:46 am

Matt, I’m using Asides as implemented in K2 in WordPress.

I’d like to know if it’s possible to control how many posts appear in a list of asides and how long it takes for them to fall off the list. Where can I control these variables?

Pingback: BUZZURRO » Asides

ThePete | January 4th, 2006 @ 6:13 am

Hi Matt, not sure if you’re still checking your comments for this post but I was wondering if there was a way to adapt this code to handle more than one category. I’d like to have a number of different layouts for a number of different categories (about five or so). I’ve tried monkeying around with the code a bit but I can’t work it out. It seems like a relatively simple prospect to me, but I could be wrong since I’m a PHP n00b. Thanks for this great tutorial regardless. I do have asides working just fine on my site. :)

Pingback: chuck reynolds

sebastian | January 7th, 2006 @ 3:18 am

thepete, are you thinking something that is more complicated than just:

if (in_category(1) && !$single) {
something;
}

if (in_category(5) && !$single) {
something;
}

i was thinking of using this blank quote to block out all photoblog entries, with access to photoblog entries only by clicking on the photoblog category link. think i’ll work? =)

Pingback: linklog | fjurl

Pingback: at Hangover Sunday

Pingback: Hi K2 at mr lim

Simran | July 3rd, 2006 @ 10:58 am

Amazing hack! I’m using it on my blog. It’s built into the great K2 theme. Thanks again man!

Lawson Digest | March 23rd, 2008 @ 7:46 am

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.

I couldn’t agree with you more. I did the same thing on my website.

Share your thoughts