If your Wordpress blog uses a home page or a landing page of some kind, featuring a random post on that page can serve a couple useful purposes. For one, it gives the reader an idea of what they can expect from your blog, especially if you make use of the Excerpt feature. Another benefit is that it helps keep your old archived posts visible. Normally people will only read the latest handful of posts, and ignore the rest. So this helps to open up the reader to even more of your valuable content.
I came across some plugins during my search, but I didn’t try any of them. I was just looking for a simple bit of code that would do the job. At first I didn’t think it would be that difficult; surely Wordpress has a random_post template tag, right? Well, they don’t. Everything I was finding required a plugin of some kind.
I happened across a post at RiteTurnOnly.com that had just what I was looking for. It requires a certain level of PHP knowledge, however – it involves adding a bit of code to The Loop.
A bare bones Loop for a page will look like this:
< ?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Basically, it keeps checking to see if there are posts to display, depending on criteria. Then it assigns formatting code to each post that meets those criteria.
Well, this random post code that I found simply adds a query_posts function and some variables to this loop. It looks like this:
< ?php query_posts(array('orderby' => 'rand', 'category_name' => SomeCategoryName, 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); ?>
When putting this into your own page code, all you have to do is change SomeCategoryName to whatever category you want to pull the random posts from. You can also change the showposts variable to however many random posts you want to show.
This will only retrieve the data – you still need to display the data. So after you start the loop, you’ll need to insert some formatting and template tags to get Wordpress to show what you want it to.
Once you’ve got that all figured out, just close the Loop:
< ?php endwhile; else: ?>
< ?php _e('Sorry, no posts matched your criteria.'); ?>
< ?php endif; ?>
This will probably take some tinkering on your part to get the output to display the way you want it to, but for ease of implementation, this Wordpress hack can’t be beat.
Thanks again, Len!





{ 6 trackbacks }
{ 31 comments… read them below or add one }
← Previous Comments
found some WordPress example about your post – http://codex.wordpress.org/Template_Tags/get_posts
Great blog you got here…keep up the good work.
WoW:)
I have recently got Sky Broadband and am now looking to connect to the router. There is a way to connect to it without using the Sky Disc which comes with it, it has been done before with my Linksys/Virgin Broadband. Can anyone help me and simply explain how? Also can you give me all the setup numbers for the router, including port numbers so that I can connect my PS3 via wire?
________________
[url=http://www.youtube.com/watch?v=AwkRBC6qzcE]unlock iphone[/url]
Thanks for this script, it worked perfectly for me.
I tried using this in a few self made themes and it worked with no problems. Then i tried just adding it to other themes and it broke each one of them. Website just showed a blank white page. Its something about the query that causing it to break. I already tried using the ‘cat’ and # but get the same results.
Great post! Thanks again!
This really helped me! Many thanks!
In other words, will AT&T also let me do that?.. Buy an unlocked iphone elsewhere (ie. online, or from a trusty companion) and not renew that 2 year contract, while adding the iphone data plan to my family plan bill?
________________
[url=http://www.youtube.com/watch?v=EpZmDnMZahc]unlock iphone 3g[/url]
that’s cool thanks for sharing
← Previous Comments