April 9 2010
Wordpress Tip: Recent, Relevant, and Random Blog Posts
12 May 2010 Update: Interestingly, Google Analytics revealed that in the month since adding the sidebar, there’s been no significant change in the average time spent on the site (54s) or pages per visit (1.3). I still like it.
Recently, I thought it would be interesting to add a sidebar to my Wordpress blog. I figured a short list of recent posts, relevant posts, and random posts would help surface information people might find useful. Here’s a quick rundown of how to add these with the PHP code below:
- Recent Posts: one-liner with simple WP PHP call
- Random Posts: a couple lines because I wanted random posts but sorted descending by date (not simply get_posts('numberposts=5&orderby=rand&order=desc');)
- Relevant Posts: also called related posts, a template-based plugin worked best for me: Yet Another Related Posts Plugin (YARPP)
</ul>
<div class="sidebar"> <b>Recent</b> <ul> <?php wp_get_archives('title_li=&type=postbypost&limit=5'); ?> </ul> <?php related_posts(); ?> <b>Random</b> <ul> <?php // Get five random posts. $rand_posts = get_posts('numberposts=5&orderby=rand'); // Sort them (by date). asort($rand_posts); // Reverse them. $rand_posts = array_reverse($rand_posts); foreach( $rand_posts as $post ) : ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </div>