Do you want to display random posts in WordPress? Displaying random posts gives your users a chance to discover more of your content. In this article, we will show you how to easily 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 (from newest to oldest). This allows users to see your latest posts first.
However, most users will not get to see your older articles. If you have been running your site for quite some time now, then your older articles will not be prominently displayed anywhere.
One way to overcome this is by making internal linking a habit. Linking to your older articles in new posts will help users discover them. It will also increase your pageviews and improve your SEO score.
Another way around that is by displaying random posts in your sidebar. This way your users will get to discover posts that they would not see otherwise.
Having said that, letβs see how you can easily display random posts in WordPress.
Method 1: Display Random Posts in WordPress with a Plugin
This method is easier and is recommended for most users.
First thing you need to do is install and activate the Advanced Random Posts Widget plugin. For more details, see our step by step guide on how to install a WordPress plugin.
Upon activation, you need to visit Appearance Β» Widgets page. You will notice a new widget labeled βRandom Postsβ under the list of available widget.
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, you can just click on the save button.
Advanced Random Posts Widget is a powerful plugin with tons of customization options. You can select different post types, show excerpt, show thumbnail, and skip posts you donβt want to show, or displays posts from specific categories or tags.
For more experienced users, the plugin also allows you to add custom before and after HTML, and your own custom CSS as well.
Donβt forget to click on the save button to store your widget settings. You can now visit your website to see random posts widget in action.
Method 2: Display 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.
First thing you need to do is add this code in your themeβs functions.php file or a site-specific plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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_shortcode' ); |
This code simply creates a function that displays 5 random posts. It then creates a shortcode so that you can easily display random posts anywhere on your site. Lastly, it enables shortcodes to be executed inside WordPress widgets so that you can use shortcode inside a text widget.
Now you can display random posts inside a WordPress post, page, or text widget using the shortcode [wpb-random-posts].
Thatβs all, we hope this article helped you learn how to display random posts in WordPress. You may also want to see these 12 WordPress sidebar tricks to increase pageviews.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.