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…

204 thoughts on “On Asides

  1. Pingback: atog » Asides
  2. Pingback: Uberwald
  3. 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/">?

  4. 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 😉

  5. 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

  6. 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.

  7. Pingback: Virulent Memes
  8. Pingback: engadgeted.net
  9. 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*

  10. 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!

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

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

  12. 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”.

  13. 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.

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

  15. Pingback: Memoirs of a Geek
  16. 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…

  17. Pingback: nico :: pinjons >>
  18. Pingback: Binary Thoughts
  19. 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???

  20. Pingback: The Wyvern's Lair
  21. Pingback: askewed » Asides
  22. Pingback: Firman Pribadi
  23. Pingback: 15june.com
  24. 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?

  25. Pingback: BUZZURRO » Asides
  26. 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. 🙂

  27. Pingback: chuck reynolds
  28. 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? =)

  29. Pingback: linklog | fjurl
  30. Pingback: at Hangover Sunday
  31. Pingback: Hi K2 at mr lim
  32. 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.

  33. I took your idea for Asides and pull in my Twitter RSS (@smjdesign) via Alex King’s TwitterTools. Twitter makes a very good way to add asides to your blog. You might consider it. Semantically–for my use, they are the same.

    I have it working on a personal website. I doubt that I will be adding it to my professional websites anytime soon due to the “off-topic-ness.” Make sure though if you bring them into your blog and subsequent RSS feed that your entire homepage isn’t full of tweets/asides!

Leave a Reply to Asides - LinjakohinaaCancel reply