My better half was complaining that she can’t find a suitable plugin for displaying random posts on her sidebar. I tried to look myself, and there were some references to the plugin but nowhere to be found. One good plugin I found is Random categories with random posts by Mike Stickel but it uses categories which is not what we are looking for. Also, it requires tinkering with PHP codes which is not suitable for less technical bloggers.
So I went to analyze his code for a little while. What the heck, I’ll just create a simple widget version with no categories involved. So I took about 30 minutes to write this simple code with not so many options. It is a widget and configurable in the Sidebar Widgets page:
- Widget title: the title of the widget
- List types: ul for bulleted list, p for paragraph, br for paragraph with line breaks
- Before title and After title: you have to find out what your theme uses before and after the widget headers. A simple way to do this is to identify a currently existing widget in your site and viewing the source. Find the title and see what is printed before and after. For example it might show as <h2>Widget Title</h2> which is used by most themes.
- Post count: Number of random posts you would like to be displayed
Basically that’s it. It’s really a simple widget.
I don’t like to reinvent the wheel but since I can’t find any I just have to create one myself. Most probably I will have not so much time to maintain this plugin, but it is suitable if you are looking for a (very) simple plugin to display random posts.
Current Version: 1.5.0
Copy the file to the wp-content/plugins/widgets directory and activate it in the Plugins page. Then drag and configure it in the Sidebar Widgets page.
Warning: Will only work on widgets enabled blogs.
- 07-May-2007: Fixed a bug of displaying widget before and after tag
- 14-Jun-2007: Fixed a bug where pages are also selected. Now it only displays posts.
- 26-Jun-2007: Removed before and after configuration options, now taken from theme settings. Was broken for some themes with embedded class in the header tags. Reported by Guohua. Thanks.
- 03-Aug-2007: Overhauled entire engine to optimize queries. This has been tested on a test blog with 50,000 posts on an old machine (Pentium III 600MHz) in maximum 3 seconds. Also removed duplicated results and added category to choose.
- 04-Aug-2007: Fixed a bug where wrong permalink was displayed.
- 10-Aug-2007: Fixed a bug where if 1 post count is selected an error occurred.
- 27-Sep-2007: Added ability to detect database version (WordPress 2.3 has database changed) while maintaining backward-compatibility with 2.2.x