Family Encyclopedia >> Electronics

How to display random posts in WordPress

Do you want to display random posts in WordPress? Displaying random posts gives users the opportunity to discover more of your content. In this article, we will show you how to easily display random posts in WordPress.

How to display random posts in WordPress

Why and where to display random posts in WordPress

By default, WordPress lists your blog posts in reverse chronological order (newest to oldest). This allows users to see their latest messages first.

However, most users will not be able to see your previous articles. If you've been running your site for quite some time, your old articles won't be prominently displayed anywhere.

One way to overcome this is by making internal linking a habit. Linking to your previous articles in new posts will help users discover them. It will also increase your pageviews and improve your SEO score.

Another way around it is to display random posts in the sidebar. This way, your users will be able to discover posts that they might not otherwise see.

With that said, let's see how you can easily display random posts in WordPress.

Video Walkthrough

Subscribe to WPBeginner

If you don't want to watch the tutorial video, you can continue reading the text version below:

Method 1:Show Random Posts in WordPress with a Plugin

This method is easier and is recommended for most users.

The first thing you need to do is install and activate the Advanced Random Posts Widget plugin. For more details, check out our step-by-step guide on how to install a WordPress plugin.

Upon activation, you need to visit Appearance »Widgets page. You will see a new widget labeled 'Random Posts' in the list of available widgets.

You need to add this widget to a sidebar. See our guide on how to add and use widgets in WordPress.

Now, you will be able to see the widget settings. The default options will work for most websites, just click the save button.

How to display random posts in WordPress

Advanced Random Posts Widget is a powerful plugin with tons of customization options. You can select different types of posts, show excerpts, show thumbnails and skip posts you don't want to show, or show posts from specific categories or tags.

For more experienced users, the plugin also allows you to add custom HTML before and after, and also your own custom CSS.

Don't forget to click the Save button to store your widget settings. You can now visit your website to see the Random Posts widget in action.

How to display random posts in WordPress

Method 2:Show Random Posts in WordPress Using Code

This method requires you to add code to your WordPress theme files. See our guide on how to copy paste code in WordPress.

The first thing you need to do is add this code to your theme's functions.php file or a site-specific plugin.

 función 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. = '
    '; while ($ the_query-> have_posts ()) $ the_query-> the_post (); $ cadena. = '
  • '. obtener_el_título (). '
  • '; $ string. = '
'; / * Restaurar datos de publicación originales * / wp_reset_postdata (); else $ string. = 'no se encontraron publicaciones'; devuelve $ cadena; add_shortcode ('wpb-random-posts', 'wpb_rand_posts'); add_filter ('widget_text', 'do_shortcode');

This code simply creates a function that returns 5 random posts. Next, create a shortcode so you can easily display random posts anywhere on your site. Lastly, it allows shortcodes to run inside WordPress widgets so you can use the shortcode inside a text widget.

You can now display random posts within a WordPress post, page, or text widget using the [wpb-random-posts] shortcode.

How to display random posts in WordPress

That's it, we hope this article helped you learn how to display random posts in WordPress. You may also want to check out these 12 WordPress Sidebar Tricks to Increase Page Views.

If you enjoyed this article, please subscribe to our WordPress YouTube Channel video tutorials. You can also find us on Twitter and Facebook.