Random Post in Current Category

As a novice WordPresser, this took ages for me to figure out, but eventually managed to solve. If you have a category and want to display one or a number of random posts from only that category then this is how you achieve it. In your archive.php under the category section use the following…

<?php query_posts(‘showposts=1&orderby=rand&cat=’.$cat); ?>
<?php while (have_posts()) : the_post(); ?>
<li><a href=”<?php the_permalink(); ?>”>
<?php the_title(); ?>
</a>  </li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

The key being…

<?php query_posts(‘showposts=1&orderby=rand&cat=‘.$cat); ?>

Spread the word. Share this post!

Leave Comment

Your email address will not be published. Required fields are marked *