Pop Culture•Music•Travel•Fashion•Sports•Money•Tech
Thursday February 16th 2012

Wordpress: How To Get A Random Post On Front Page

I thought I would post this tip since it might be of interest to to anyone running a wordpress blog.

Wordpress has become the most popular blogging software. Normally a blog displays the posts on the front in normal blog order, which is reverse chronological order. Kind of like a diary. I know many bloggers would prefer to have the blog posts somewhere besides right on the front page. Maybe they want a “splash” page, or a page with static information.

Wordpress allows you to have a static front page for you website.

Getting A Random Post On Front Page
Suppose you have been blogging for a while and have a lot of blog posts in various categories in your archives. Suppose you would like your front page to show one sample post chosen randomly from your category archives. Here is how to do it.

First you have to locate the index.php in your themes directory. This would be at something like /wordpress/wp-content/themes/mytheme directory. Save a backup and edit index.php using your favorite text editor, like vi or BBEdit.

Find just before you enter The Loop. Use query_post with the parameter orderby=rand to select one random post from several categories. You have to know the category id numbers, which in this case are 100, 101 and 102.

<?php query_posts('orderby=rand&showposts=1&cat=100,133,155,156&paged=$page'); ?>
<?php if (have_posts()): ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>

The parameter showposts=1 displays only one post. The parameter paged=$page makes the Next and Previous links work right.

Using your ftp client, upload the new index.php to your webserver. On the Macintosh, Cyberduck is one ftp program you can use.

That is all there is to it. Every time the page gets loaded, there will be a new random post on your front page.

AddressMunger.com munges your text so you can Display HTML/Javascript/php code on a webpage.

More from category

Unix: How to Automate Thumbnails
Unix: How to Automate Thumbnails

Use a shell script to automate the creation and uploading of thumbnails. [Read More]

Wordpress: How to Disable Post Revisions
Wordpress: How to Disable Post Revisions

By disabling post revisions in Wordpress, it reduced the database size and allowed the wordpress MySQL database backups [Read More]

Wordpress: How to Target Ads for Different Categories
Wordpress: How to Target Ads for Different Categories

Display different ads in each categories in your Wordpress blog. [Read More]

LBB 2010 Annual Report to Shareholders
LBB 2010 Annual Report to Shareholders

2010 Web Server Report for Laguna Beach Bikini [Read More]

LBB Webserver Status Bulletin

LBB was offline due to corrupted mySQL database. The tables were repaired and the blog is back online [Read More]

Advertisement

LBB Stats