How to Display Random Posts in WordPress without a Plugin

Share This Article:

Ever though of displaying Random posts in WordPress ? Then this tutorial on how to display random posts in WordPress without a plugin is for you. using too many plugins may effect your blog loading time and it is recommended to go with code, wherever simple function is required.

random-posts-in-wordpress-without-plugin

This php code can be used inside text widget, read How to Execute php in WordPress Text Widget for more info.

Display Random Posts in WordPress

Use this code in any part of your theme to display random posts.

<div>
<h2>Random Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
</div>

Functionality of this Code:

  • It will simply display a list of random posts in your blog.
  • Code used here displays 5 random posts and that value can be changed.
  • Change the value of 'numberposts' => 5 to any positve integer.

Read This : How to Disable or Limit Post Revisions in WordPress

Display Random Posts with Excerpt in WordPress

Use this code in any part of your theme to display random posts.

<div>
<h2>Random Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><p><?php the_excerpt(); ?></p></li>
<?php endforeach; ?>
</ul>
</div>

Functionality of this Code:

  • It simply displays a list of random posts along with post-excerpts.
  • Code used here displays 5 random posts and that value can be changed.
  • Change the value of 'numberposts' => 5 to any positve integer.

Also Check : How to add 3 column footer to WordPress theme

Styling Random Posts in WordPress with CSS

After adding the random posts code, its time to style them. Add the below code to style.css file in your WordPress theme.

.random-posts {
margin: 10px 0;
}
.random-posts p{
font-size:13px;
}
.random-posts h2{
font-size: 16px;
font-weight: bold;
}
.random posts ul {
list-style: square;
margin: 10px 0;
}
.random posts ul li {
padding: 3px 0;
border-bottom: 1px dashed #ccc;
}
.random posts ul li a {
font-size: 14px;
text-decoration: none;
}

You have successfully added random posts in WordPress blog without a plugin, hope you like this tutorial and if you got any query on this, please do drop your valuable comments.

Genesis is the most flexible WordPress theme framework that comes with tons of options. We have been using Genesis on all of our blogs and recommending the same to you. Every bit of it is coded according to the standards. Give it a try!

Comments

  1. Raaj Trambadia says:

    Hello! Actually, I’m a bit confused. When you say ‘Display Random Posts’, do you mean on the homepage? Or is it on the sidebar? Please advice FOR THE THEME I AM USING. Please check my site and advice me on what should I do? Because I have the ‘Slider on the top’ where I can choose the category I want to display posts from, hence I’ve made a category called ‘Featured’ and have just put 4 posts in it. But then, do you think that showing those posts even on the home page looks good? Or should those posts on the homepage be different than those in the featured slider?

    Thanks!

    • WPSquare says:

      Using this code Random Posts can be displayed on homepage, single post page or in sidebar. And, these Random posts are not based on “categories”.

  2. Sohil R. Memon says:

    Awesome Work!
    First Method is too Simple and I have done on my Blog.
    Nice theme too.

    Rehards,
    Sohil Memon.

  3. Computer Security Tips says:

    Awesome tutorial.Let me try it with my blog also.Thanks

Speak Your MindComments Policy →

*

Looking for More Awesomeness?

Read previous post:
execute php in wordpress text widget without plugin
How to Execute php in WordPress Text Widget without a Plugin

Default text widget in WordPress doesn't posses the capability of executing PHP code. If you want to execute php code...

Close