making bbPress (and WordPress) work better!

Show Off Your WordPress Archives With Random Posts

I have a couple of blogs with posts now in the hundreds. Some of them are timeless articles, but often visitors won’t go digging that deep. So I started searching for a way to make a “Random” selection on the sidebar, under archives or categories so they could sample across the board.

After looking at half a dozen plugins to do this, I was fustrated that it was so messy. Once I picked one I thought I could live with, I found that I had to rewrite my template too extensively, changing the loop and it just didn’t feel like a good idea for the future.

Then after alot of Googling, I found this gem on the WP hackers list:

Simply save that into your plugins and activate, then in your template files simple intercept a request for the “Random” category and inject a new list of posts before the loop with a simple call like: query_posts(‘random=true’)

Here’s a more full example:

[?php /* If this is a category archive */ if (is_category()) {
if ((strtoupper(single_cat_title('',0))=="RANDOM")) { query_posts('random=true'); ?]
[h2 class="pagetitle"]Random Articles From Our Archives[/h2]
[? } else { ?]
[h2 class="pagetitle"]Archive for '[?php single_cat_title(); ?]'

(change [ ] to < >)
I had to create the random category but since it has zero real entries, it won't show up in the category list. So I manually inserted it into the sidebar.
[a href="/blog/category/random/"]Random Articles[/a]

One response

  1. Just wanted to say thanks for this. Seems to work nicely on my new site (which isn’t live just yet) so cheers!

    October 9, 2007 at 7:37 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s