WordPress: Show random posts

Sometimes you want to show a list of random posts from your WordPress blog/website to your readers. It can be a great way of engaging readers for a longer duration by offering them more content. How to show a list of random posts in WordPress?

Copy the code snippet below and paste it to sidebar.php or the desired WordPress theme file where you want to show the list of random posts.

<?php
$my_query = new WP_Query('orderby=rand&showposts=5');
while ($my_query->have_posts()) : $my_query->the_post();

if ( has_post_thumbnail() ): ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark">
<?php echo get_the_post_thumbnail( $post->ID, 'thumbnail', array('class' => 'thumbnail') ); ?>
</a>
<?php endif; ?>

<h3><a href="<?php the_permalink(); ?>" title="<<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<p><?php the_time('j M Y'); ?></p>
<p><?php comments_popup_link('0 Comment', '1 Comment', '% Comments'); ?></p>

<?php endwhile; ?>

Premium WordPress Themes

Reliable Web Hosting

Smart Web Worker strongly recommends HostGator for your website/blog hosting. Sign up for web hosting at just $3.96/month. Use the coupon code "SMARTBLOGGER25" to get 25% discount on any hosting packages. Get an account with HostGator now!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>