How to List Random Posts in a WordPress Blog’s Sidebar

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 MariaLanger.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:
<?php if (is_category()) { ?>
<h2><?php single_cat_title('Random Articles in '); ?></h2>
<ul>
<?php c2c_get_random_posts(16,"<li>%post_URL%</li>", $wp_query->query_vars['cat']); ?>
</ul>
<?php } ?>
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
<?php c2c_get_random_posts(16,"<li>%post_URL%</li>", $wp_query->query_vars['cat']); ?>
with
<?php c2c_get_recent_posts(150,"<li>%post_URL%</li>", $wp_query->query_vars['cat']); ?>
Remember to make the value big enough to cover all the posts in each category. I don’t know if there’s a limit to the value, but I probably wouldn’t use this if I had more than a hundred or so posts in a category anyway. It would make the sidebar too long. (My opinion.)
Of course, you might want to change the heading, too — something like
<h2><?php single_cat_title('All Articles in '); ?></h2>
would be more appropriate when showing all the posts.
As you can imagine, there are lots of ways you can play with this to list post titles in the sidebar. And if your post titles are intriguing enough, these few lines of code might keep visitors on your site a bit longer.
Reorganizing WordPress Categories

I decided the other day that some of my blog’s categories were too similar to others (for example, Flying for Hire, Flying for Pleasure, and Flying Lessons) and that I wanted to trim down my category list to make room for new categories in the future. So I combined a few categories and added one.
When I combined Writing for a Living and Writing for Pleasure into one category (Writing), I did it the hard way: I edited all the posts in Writing for Pleasure to use the Writing for a Living category. Then I changed the Writing for a Living category’s Category Name to Writing and the Category Slug to writing. I renamed Writing for Pleasure as a new category called On Blogging with a category slug of blogging. This was extremely time consuming, since I had to modify each individual post and was working from home, with a miserable 256Kbps connection.
For the three flying categories, I decided to get fancy. I changed the name and slug of Flying for Hire, which had the most posts, to Flying and flying. Then I changed the default category (Options > Reading) to Flying. Then I deleted the Flying for Pleasure and Flying Lessons categories, clicking OK in the warning dialog that appeared for each one to allow the default category (Flying) to be assigned to each. This was certainly a faster way to get the job done, but it gave me quite a scare when the number of posts in the Flying category did not increase and the total post count decreased. Had WordPress actually deleted posts? I had to check the contents of that category to make sure I had posts from all three categories in it. I do, but the inconsistent count has me worried, so I don’t necessarily recommend doing what I did to combine categories. Ask me in a week or two and I’ll let you know what I think.
Of course, changing category slugs isn’t a good thing to do when you’re using one of the permalink options (Options > Permalink) to make “friendly” URLs. My changes affected five categories; any links to the old category slugs would break. So I opened my .htaccess file and added the following lines near the top:
Redirect permanent /category/weblog/writing-for-a-living/ http://www.marialanger.com/category/weblog/writing/
Redirect permanent /category/weblog/writing-for-pleasure/ http://www.marialanger.com/category/weblog/writing/
Redirect permanent /category/weblog/flying-for-pleasure/ http://www.marialanger.com/category/weblog/flying/
Redirect permanent /category/weblog/flying-for-hire/ http://www.marialanger.com/category/weblog/flying/
Redirect permanent /category/weblog/flying-lessons/ http://www.marialanger.com/category/weblog/flying/
Keep in mind here that all of the categories I changed are actually subcategories of the Maria’s Weblog category, which has a slug of weblog. Each of these lines automatically redirect the old category URL to the new one. You can learn more about .htaccess at one of my favorite online sources: Stupid .htaccess Tricks.
The net effect of all these changes? Five categories have been combined into two and a new category has been created. My links should continue to work as they did.
Now let’s just hope I didn’t lose any posts or screw up that .htaccess file…
Protect Your Content

If you offer valuable content on your Web site or blog, you may be concerned about someone stealing your work for their own gain. Or worse yet — passing it off as their own work. This applies whether you’re managing a relatively static Web site or publishing new content regularly on a blog.
From “How to Protect Your Website’s Copyright When Someone Steals Your Content“:
There is nothing worse for a webmaster than finding out that your site has been copied. Every time I find out that someone has stolen my intellectual property my blood boils! Instead of just getting angry, there are steps you can take to stop the violations of your Copyright, which I’ve outlined in this article.
The article discusses some good tools for finding violations and doing something about them.
Technorati Tags: blogging, copyright, website
Adding Del.icio.us and Digg Links to Your WordPress Posts

Look, Mom! No plugins!
Although there are WordPress plugins that enable you to add links to bookmarking sites from your posts, if you’re just interested in a few sites and don’t want to deal with plugin overhead (or future compatibility), there is an easier way. Just add the links to your WordPress theme’s template file, inside The Loop.
The two bookmarking sites I’m interested in are Del.icio.us and Digg. I use Del.icio.us for bookmarking primarily because of its Daily Blog Posting feature, which I wrote about in my “Del.icio.us Links” article. (The terminology for the feature has changed since I wrote the article; it can now be found under Daily Blog Posting in the Settings area for your Del.icio.us account.) This feature displays a list of the posts I bookmark with Del.icio.us each day. You can find examples under This Just In… on my site.
I don’t regularly use Digg, but I do have an account. Digg got a lot of press a while back, but it appears to have become a popularity contest, with people promoting their own posts and voting for their friends. Still a lot of people use Digg, so it’s good to support it.
The links I added to my site enable a user to quickly bookmark a post on Del.icio.us or Digg. The visitor clicks one of the links and goes right to the appropriate page for entering the bookmark information. Finishing up on that page brings them back to your site.
Here are the two links; you can copy and past them into your template file, within The Loop:
<a href="http://del.icio.us/post?url=<?php the_permalink(); ?>&title=<?php the_title() ?>" title="Add this post to del.icio.us">Add to Del.icio.us</a>
<a href="http://digg.com/submit?phase=2&title=<?php the_title() ?>&url=<?php the_permalink(); ?>" title="Digg This">Digg This</a>
Obviously, the text that appears for the link can be completely customized for your needs. Ditto for the title attribute.
You can see an example on any post on my site, www.marialanger.com. You’ll find the links at the bottom of each post, along with links for entering comments, printing the post, and a display of the number of times a post has been read:

If you’re not sure what The Loop is or how to modify a template file, you need our book, WordPress 2: Visual QuickStart Guide. It devotes an entire chapter to modifying theme template files. You can learn more about the book on its companion Web site, www.wpvqs.com.
WordPress Guest Book Plugin

I decided a few minutes ago that I wanted to add a guest book to my personal site. A place where people could enter their comments about the site in general. I get a lot of comments in Feedback via the Comment form, but I really prefer that they be made on the site itself, where others can read them, too.
So I Googled wordpress guest book plugin and I came up with Alleba Blog ยป Wordpress Guestbook Generator Plugin.
This plugin is an excellent tool for creating a quickie guest book. Install it like any other plugin, activate it, and then click a button on its configuration page. It automatically generates a page template and comment template for displaying your guest book and the entries your site visitors may leave. Since the templates are based on your active theme, they look like they belong on your site. And since they’re templates, they’re easy enough to tweak if you need to. (I, of course, tweak everything because I’m never satisfied with the way things look or work right out of the box.) And since the guest book utilizes the comment feature, all entries are automatically put through your comment moderation tools.
It took me about 10 minutes to install, activate, and tweak the new guest book. And another two minutes to add it to the page navigation menu at the top of every page of this site.
I invite you to give it a try.
Many thanks to Andrew dela Serna for creating such an easy-to-use tool!
ProBlogger Group Writing Project Contest

ProBlogger is sponsoring another Group Writing Project contest, with prizes worth between $2,000 and $2,500. You can learn more at ProBlogger Group Writing Project - Reviews and Predictions.