How to List Random Posts in a WordPress Blog’s Sidebar
By
Maria Langer | Posted December 31st, 2006
Draw people into your blog by showing them other posts in the same category.
People browse blogs all kinds of ways. One way is to come in at the Home page and browse what's there, then click a category link in your navigation bar to go deeper into the site. Clicking a category link identifies a topic that interests them. But how can you get them to keep browsing what's there when all they'll see is the few most recent posts displayed there?

One way is to display a random list of category posts in the sidebar (see screenshot right or
visit this page for an example). There are a number of ways to do this, but the quickest and easiest (which is usually my preferred method) is to use the
Customizable Post Listings plugin by
Scott Reilly with a few simple lines of code inserted in your sidebar.
Customizable Post Listings isn't new. Its latest version (1.1) was released back in 2004. But it still works like a charm -- at least in WordPress 2.0.4 on my server. From the description:
Display Recent Posts, Recently Commented Posts, Recently Modified Posts, Random Posts, and other post listings using the post information of your choosing in an easily customizable manner. You can narrow post searches by specifying categories and/or authors, among other things.
I already use it on
aneclecticmind.com to display recent posts and recently commented posts, so adding another feature that takes advantage of this plugin wasn't a big deal. In fact, it made sense.
I won't go into details about how to download and install the plugin. You can figure that out for yourself. If you're new to WordPress and plugins and need help, I will plug the book I co-authored with Miraz Jordan here:
WordPress 2: Visual QuickStart Guide. It has a whole chapter dedicated to plugins. Enough said.
Once the plugin has been installed and activated, open your
sidebar.php file (or the file in which you want to include the random post code). Then enter the following code where you want the list of random posts to appear:
Keep in mind that this code uses the tags I use in my sidebar for formatting:
h2 for sidebar headings and
ul for an unordered list. Your HTML formatting may differ, so you might have to adjust this a bit for your site.
The first and last lines of this code provide conditional instructions. They basically say that if the page being displayed is a category page, show the random posts listing. So if the Home page or a single post page is displayed, the random category list does not appear.
The
single_cat_title tag is a neat way to customize the heading so it displays the name of the category.
You might also want to change the value in the
c2c_get_random_posts tag. I have it set to show 16 posts; you can show more or fewer. If you put a value in there that's higher than the number of posts in the category, all of the posts will be displayed in random order. But if you want to display all of the posts, why not do it in reverse chronological order? Just replace
Fatal error: Call to undefined function c2c_get_random_posts() in /home2/aneclect/public_html/mariasguides/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code on line 32