Navigation is a big issue with *alot* of people. People want to find your content or find a way to ‘get’ to your content. One of ways you can do that is to actually have something that makes it so that users can go to a post of yours that’s completely random.

In ComicPress, Tyler Martin created a Random Comic widget which is very useful to have people go to any number of comics in the archive. I’ve adapted the widget to be random posts, a pick of all of them and converted it to a WordPress 2.8 + widget.

Let’s start off by writing the line of code that looks for a word in the URI line:

<br />
if ( isset( $_GET['randompost'] ) )<br />
	add_action( 'template_redirect', 'random_post' );<br />

What this line does it looks for /?randompost at the end of a URI line and adds an action to the template_redirect function that executes the function random_post. Because of the questionmark before randompost the uri line will think that it’s a uri variable container and wants a variable for it, but we dont need one, just setting it is enough.

Now let’s write the function random_post that gets run when it finds randompost in the uri line.

<br />
//Generate a random post page - to use simply create a URL link to &amp;quot;/?randompost&amp;quot;<br />
function random_post() {<br />
	$randomPostQuery = new WP_Query(); $randomPostQuery-&amp;gt;query('showposts=1&amp;amp;orderby=rand');<br />
	while ($randomPostQuery-&amp;gt;have_posts()) : $randomPostQuery-&amp;gt;the_post();<br />
		$random_post_id = get_the_ID();<br />
	endwhile;<br />
	wp_redirect( get_permalink( $random_post_id ) );<br />
	exit;<br />
}<br />

This function random_post creates a new query since $wp_query and $post are not being read as a global will totally avoid it. In the query line it asks for 1 post and orders it by rand which means randomize the entire posts archive but only show one post. Then what it does it does a while loop of all the posts it finds and sets $random_post_id to the ID of the post. Then redirect by getting the permalink that $random_post_id points to.

Okay so far we’ve got this, we’ve got the ability to read randompost from the URI line with a $_GET and then we have it execute the function to actually redirect users to a random post on your system.

But how do we embed it on your site? Well you could just make a static link that goes Random Post that would work just fine, because all your doing is wanting to set the URI line. You can do that with a text widget very easily. … I wrote a WordPress 2.8/2.8.4+ widget that will do it for you however so you can just adjust it’s position in your sidebars how you see fit.

<br />
class widget_random_post extends WP_Widget {</p>
<p>	function widget_random_post() {<br />
		$widget_ops = array('classname' =&amp;gt; 'widget_comicpress_random_post', 'description' =&amp;gt; 'Displays a link to click to trigger a random blog post.' );<br />
		$this-&amp;gt;WP_Widget('random_post', 'Random Post', $widget_ops);<br />
<p>	function widget($args, $instance) {<br />
		global $post;<br />
		extract($args, EXTR_SKIP);</p>
<p>		echo $before_widget;<br />
		$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);<br />
		if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }; ?&amp;gt;<br />
			&amp;lt;h2&amp;gt;&amp;lt;a href=&amp;quot;?randompost&amp;quot;&amp;gt;&amp;lt;span class=&amp;quot;random-post-icon&amp;quot;&amp;gt;?&amp;lt;/span&amp;gt; Random Post&amp;lt;/a&amp;gt;&amp;lt;/h2&amp;gt;<br />
		&amp;lt;?php<br />
		echo $after_widget;<br />
<p>	function update($new_instance, $old_instance) {<br />
		$instance = $old_instance;<br />
		$instance['title'] = strip_tags($new_instance['title']);<br />
		return $instance;<br />
<p>	function form($instance) {<br />
		$instance = wp_parse_args( (array) $instance, array( 'title' =&amp;gt; '' ) );<br />
		$title = strip_tags($instance['title']);<br />
		?&amp;gt;<br />
		&amp;lt;p&amp;gt;&amp;lt;label for=&amp;quot;&amp;lt;?php echo $this-&amp;gt;get_field_id('title'); ?&amp;gt;&amp;quot;&amp;gt;Title: &amp;lt;input class=&amp;quot;widefat&amp;quot; id=&amp;quot;&amp;lt;?php echo $this-&amp;gt;get_field_id('title'); ?&amp;gt;&amp;quot; name=&amp;quot;&amp;lt;?php echo $this-&amp;gt;get_field_name('title'); ?&amp;gt;&amp;quot; type=&amp;quot;text&amp;quot; value=&amp;quot;&amp;lt;?php echo attribute_escape($title); ?&amp;gt;&amp;quot; /&amp;gt;&amp;lt;/label&amp;gt;&amp;lt;/p&amp;gt;<br />
		&amp;lt;?php<br />
	}<br />
}<br />
<p>function widget_random_post_init() {<br />
	new widget_random_post();<br />
<p>add_action('widgets_init', 'widget_random_post_init');<br />

This is a standard 2.8 widget. You can then go to Appearance -> Widgets and add it to any of your sidebars.

Just like Tyler did with ComicPress’s Random Comic I set a class inside the widget so you can place an icon image inside there if you wanted to, or in this case just make the ? mark look nicer.

<br />
&amp;lt;span class=&amp;quot;random-post-icon&amp;quot;&amp;gt;?&amp;lt;/span&amp;gt;<br />

<br />
.random-post-icon {<br />
	padding: 0 5px;<br />
	color: #fff;<br />
	background: #000;<br />
}<br />

You of course can change the CSS to look how you want to for an icon.

Have fun!

– Phil (Frumph)