How to Display Random Posts On Your WordPress Blogs

How to Display Random Posts On Your WordPress Blogs

posted in: Blog, Plugins, WordPress | 0

Random posts are useful because they will help you display your older posts. These posts might be of interest to the user and he might click on it. Improving the view count of older posts also adds to its SEO. There are many ways to display random posts on your WordPress websites. We have chosen the simplest ones for you.

  1. Display Random Posts in WordPress Using a Plugin

The easiest method to display random posts is to use a plugin. There are again many plugins in the WordPress Plugin Directory for you to choose from. The best one, however, is the Advanced Random Posts Widget plugin.

The plugin works as a widget. You can place it on your sidebar or any other widget area. However, it best looks on the sidebar or the footer area. Download and install the plugin. Once it’s done, activate it. Now, move to the Appearance >> Widgets section on your dashboard.

You should be able to find the Advanced Random Posts Widget there. Drag the widget to the sidebar. Once this is once, you can see the ‘Widget Settings’ option. You can now see many customization options like ‘number of posts’, ‘ignore sticky posts’ etc. Choose them as you please. If you are an advanced user, you have options for custom CSS as well.

display random posts 1

Once you choose your options, don’t forget to click on ‘save’. If you refresh your site, or clear the cache, you should be able to see the random posts in the sidebar.

display random posts 2

  1. Display Random Posts in WordPress Using a Snippet of Code

You can also use codes to display random posts in WordPress. However, do not try this if you are not an advanced user. This the code you will have to copy and paste in WordPress.

function wpb_rand_posts() { 

$args = array(
  'post_type' => 'post',
  'orderby'	=> 'rand',
  'posts_per_page' => 5, 
  );

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {

$string .= '<ul>';
  while ( $the_query->have_posts() ) {
    $the_query->the_post();
    $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
  }
  $string .= '</ul>';
  /* Restore original Post Data */
  wp_reset_postdata();
} else {

$string .= 'no posts found';
}

return $string; 
} 

add_shortcode('wpb-random-posts','wpb_rand_posts');
add_filter('widget_text', 'do_shortc

 

You should use the shortcode [wpb-random-posts] anywhere on the site to display 5 random posts from your website.

These are the easiest ways to display random posts on your WordPress site. For more WordPress tutorials and tips on web development and business, subscribe to Philoveracity.com.

 

Follow Editorial Staff:

Editorial Staff

Producing articles, creating content & other great material for the BEST Web Agency in Southern California.

Leave a Reply