How to Easily Display Random Posts on Your WordPress Website
Most WordPress sites display blog posts in reverse chronological order, with the newest posts appearing first. This makes sense, as your latest content is usually the most relevant and keeps your site looking fresh.
However, if you‘ve been blogging for a while, you likely have an extensive archive of older posts that are still valuable but don‘t get much traffic anymore. That‘s where displaying random posts comes in. By showcasing a random selection of posts across your site, you can pique visitors‘ interest, make your archives more discoverable, and extend the lifespan of your evergreen content.
In this guide, we‘ll walk through exactly how to set up random post displays on your WordPress site. Whether you want to manually add code or use a plugin, you‘ll learn multiple methods so you can choose the approach that works best for your needs.
Benefits of Displaying Random Posts in WordPress
Before we dive into the technical details, let‘s discuss some of the key advantages of featuring random posts on your site:
Engage Visitors and Reduce Bounce Rates
When visitors discover an intriguing headline or image in your random posts, they‘re likely to click through and view more of your content. This increases pageviews, reduces bounce rates, and keeps users on your site longer. More engagement signals to search engines that your site is valuable and may boost your rankings.
Drive Traffic to Older Content
Your newer posts naturally attract the most traffic, especially if you‘re actively promoting them. But your old posts shouldn‘t be gathering cobwebs – they‘re still relevant and useful to your audience. Displaying random posts ensures your entire archive has a chance to be seen and clicked on.
Highlight Your Best Content
Have an epic post that you want to keep sending traffic to? With random post widgets, you can give extra weight to selected posts to increase the chances they‘ll be displayed. This allows you to resurface your cornerstone content periodically.
Improve On-Site SEO
Internal links are crucial for SEO, as they help search engines understand your site structure and distribute link equity. By linking to other posts naturally via your random post displays, you create a more interconnected site that‘s easier for search engines to crawl and rank.
Now that you know the ‘why‘, let‘s get into the ‘how‘. We‘ll start with the manual method of adding a random posts shortcode.
Manually Add Code to Display Random Posts
This method involves adding code directly to your theme‘s functions.php file. It‘s a good approach if you only want to display a simple list of random post titles. However, it requires editing code, so it‘s better suited for more tech-savvy users.
To get started, you‘ll need to access your WordPress files. If you‘re not sure how, contact your hosting provider for instructions specific to your hosting account.
Once you‘ve accessed your files, navigate to your currently active theme‘s folder and open the functions.php file in a text editor. Paste the following code at the bottom of the file, before the closing ?> tag:
function display_random_posts() {
$args = array(
‘post_type‘ => ‘post‘,
‘orderby‘ => ‘rand‘,
‘posts_per_page‘ => 5
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$random_posts = ‘<ul>‘;
while ( $the_query->have_posts() ) {
$the_query->the_post();
$random_posts .= ‘<li><a href="‘ . get_permalink() . ‘">‘ . get_the_title() . ‘</a></li>‘;
}
$random_posts .= ‘</ul>‘;
wp_reset_postdata();
return $random_posts;
}
}
add_shortcode(‘random_posts‘, ‘display_random_posts‘);
This code does the following:
- Creates a new function called display_random_posts
- Defines arguments to retrieve 5 random posts
- Starts a new query to get posts matching those arguments
- Loops through the queried posts and builds an HTML unordered list with post titles linking to the post URL
- Closes the list, resets the query, and returns the list of random posts
- Registers a shortcode [random_posts] that will execute the display_random_posts function
After adding this code, save the functions.php file. You can now display a list of 5 random posts anywhere on your site by adding the [random_posts] shortcode in a post, page, or text widget.
If you want to change the number of posts displayed, find this line in the code:
‘posts_per_page‘ => 5
Simply change the number 5 to however many posts you want to show. Keep in mind that the more posts you display, the longer the list will be.
That‘s it for the manual method! It‘s a relatively quick way to add a basic random posts list to your site.
However, if you want more flexibility over the appearance and placement of your random post displays, using a plugin is the way to go. In the next section, we‘ll use a popular free plugin to unlock more customization options.
Use a Plugin to Display Random Posts
For most users, the easiest way to display random posts is by installing a WordPress plugin. There are a number of plugins dedicated to this purpose, but we‘ll use the Advanced Random Posts Widget in this tutorial.
This free plugin is simple to configure and offers tons of options for customizing how your random posts appear – including thumbnail images, excerpts, post dates, and more. You can create multiple random post widgets with different settings, and place them in your sidebar or anywhere else using the provided shortcode.
Here‘s how to get started with the Advanced Random Posts Widget plugin:
Step 1: Install and Activate the Plugin
In your WordPress dashboard, go to Plugins > Add New and search for "Advanced Random Posts Widget". Click "Install Now" and then "Activate".
Step 2: Configure the Widget Settings
After activating the plugin, go to Appearance > Widgets. Find the "Advanced Random Posts" widget and drag it into your desired sidebar.
The widget settings panel will expand, revealing six tabs with various configuration options. Most of the settings are self-explanatory, but here‘s a quick overview:
General: Set the widget title, number of posts to show, and sorting options.
Posts: Choose which post types to include/exclude, limit posts by age, and select individual posts to feature.
Taxonomy: Filter posts by categories, tags, or custom taxonomies.
Thumbnail: Choose a thumbnail source, size, alignment, and default thumbnail.
Extras: Show/hide post title, date, excerpt, and thumbnail, and configure excerpt settings.
Custom CSS: Enter custom CSS to style the widget.
The great thing about this plugin is that you can create multiple random post widgets, each with their own unique settings. For example, you could have one widget displaying your most popular posts with a thumbnail and excerpt, and another widget showing a basic list of random posts from a specific category. The possibilities are endless!
Once you‘ve configured the widget to your liking, click "Save". Visit your site to see your random posts widget in action.
Step 3: Display Random Posts with a Shortcode
In addition to the widget, the Advanced Random Posts plugin also provides a shortcode to display random posts anywhere on your site.
To use the shortcode, simply add [arpw] to any post, page, or text widget. The shortcode will inherit the settings from the first Advanced Random Posts widget you created.
If you want to customize the shortcode output, you can add parameters like this:
[arpw limit="3" thumbnail="false" excerpt="true"]
This would display 3 random posts with the excerpt shown but thumbnail hidden. Check the plugin documentation for a full list of available shortcode parameters.
Tips and Tricks for Using Random Post Displays
Now that you know the basics of setting up random post displays, here are some tips to make the most of this feature:
Use Eye-Catching Thumbnails
Choose attractive, relevant featured images for your posts. When users see an intriguing thumbnail, they‘ll be more likely to click through to read the full post.
Write Compelling Excerpts
If you‘re displaying post excerpts, make sure they‘re engaging and leave readers wanting more. Avoid using the first few sentences of your post as the excerpt, as they may not provide enough context. Instead, write a custom excerpt that clearly conveys the post‘s main idea and encourages users to click through.
Maximize Relevance with Taxonomy Filters
If a post is about a specific topic, you can increase the relevance of your random post suggestions by filtering for the same category or tag. This is especially useful when displaying random posts at the end of a post. By showing related content, you keep users engaged and on your site longer.
Exclude Certain Posts
Some types of posts, like announcements or seasonal content, may not make sense to display randomly. Use the Posts tab in the widget settings to exclude specific posts or limit the pool of random posts to only those published within a certain timeframe.
Conduct A/B Tests
Try different widget variations to see what gets the most engagement. Use a tool like Google Optimize to A/B test your random post displays and optimize their placement and design for clicks.
Troubleshooting Common Issues
If you‘re having trouble getting your random post displays to work, here are some common issues and solutions:
Random posts aren‘t showing up: Double-check that you‘ve added the shortcode correctly or that the widget is properly placed in your sidebar. If you‘re using the manual code method, make sure the code is pasted correctly in your functions.php file.
Thumbnail images aren‘t displaying: Ensure that your posts have featured images set. If you‘re using the plugin, check that you‘ve selected the correct thumbnail source and size in the widget settings.
Styling looks broken: If your random post displays look odd, there may be a conflict with your theme‘s styles. Try adjusting the widget‘s settings or adding custom CSS to override the theme styles.
Conclusion
Displaying random posts is an effective way to drive more traffic to your older content and keep visitors engaged on your site. Whether you choose to add a simple list of posts manually or use the Advanced Random Posts Widget plugin for more flexibility, the setup process is fairly straightforward.
By following the tips in this guide and experimenting with different placements and configurations, you can find the perfect random posts solution for your WordPress site.
