How to show Random posts without using any WordPress plugins

Displaying the “Random posts” in your blog is the best ways to drag the users/ viewers attentions and hence helps in optimizing the bounce rates of your website. There are many plugins which display the random posts like Random Posts Widget ,Advanced Random Post ,Random Posts ,AJAX Random Posts ,Random Posts from Category ,Randomizer ,Random Featured Post and several others. Apart from these plugins you can also use light custom code to get the random posts which work on simple MySQL RAND() function to sort the random post using parameter values.

Code for displaying random posts in wordpress template

<ul>
  <li>
    <h2>Random thougths of my blog</h2>
    <ul>
      <?php
 $rand_posts = get_posts(‘numberposts=5&orderby=rand’);
 foreach( $rand_posts as $post ) :
 ?>
      <li><a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>
        </a></li>
      <?php endforeach; ?>
    </ul>
  </li>
</ul>

Showing Posts from categories

<ul>
  <li>
    <h2>Random</h2>
    <ul>
      <?php
 $rand_posts = get_posts(‘category_name=Uncategorized’,’numberposts=5′,’orderby=rand’);
 foreach( $rand_posts as $post ) :
 ?>
      <li><a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>
        </a></li>
      <?php endforeach; ?>
    </ul>
  </li>
</ul>

Note: You can use the above code anywhere in your wordpress templates. If you like this post then must go through the 101 List Of Awesome WordPress Tips and Resources .