Hey there, WordPress wizard! π§ββοΈ Are you looking to add some serendipity to your site and encourage visitors to explore more of your fabulous content? Displaying random posts is a surefire way to pique your readers‘ curiosity and boost engagement.
Think about it: you‘ve poured your heart and soul into crafting killer content, but once a post slips off the first page, it‘s easy for it to get lost in the archives. That‘s a shame, because even your older articles have tons of value for the right reader.
By featuring random posts on your site, you give those hidden gems another chance to shine. π Your visitors will appreciate the unexpected discoveries, and you‘ll get more eyeballs on your best content. It‘s a win-win!
Why You Should Display Random Posts
Still not convinced? Let‘s look at some cold, hard data on why showing random posts is a smart move:
- Websites that feature suggested content, including random posts, see an average 15% increase in pageviews per visitor compared to those that don‘t. (source)
- Displaying related or random posts can increase time on site by up to 25%, as visitors click through to explore more content. (source)
- Over 60% of readers say they‘re more likely to return to a site that helps them discover new content they‘re interested in. (source)
The numbers don‘t lie β featuring random posts can have a huge impact on your site‘s stickiness and engagement. But how do you actually do it? Don‘t worry, I‘ve got you covered. πͺ
Method 1: Use the Recent Posts Widget Extended Plugin
If you‘re looking for a quick and code-free way to display random posts, the Recent Posts Widget Extended plugin is your new best friend. Here‘s how to get it up and running:
- Head to the Plugins section of your WordPress admin dashboard and click "Add New".
- Search for "Recent Posts Widget Extended" and look for this one by Satrya:
- Click "Install Now" and then "Activate". Sweet, the plugin is now ready to rock! πΈ
- Navigate to Appearance > Widgets in your dashboard. Find the "Recent Posts Extended" widget and drag it to your desired widget area, like the sidebar:
- Now comes the fun part β customizing your random posts display! First, give the widget a snappy title like "Feeling Lucky? π" or "Take a Chance on These Posts!".
- Next, set the "Order by" dropdown to "Random". This is the magic ingredient for randomized goodness.
- Play with the other settings to get the look you want. You can show thumbnails, tweak the excerpt length, and more:
- Click "Save" and behold your shiny new random post widget on your site! π
Recent Posts Widget Extended makes it a cinch to add random posts to your sidebar or footer. But what if you want them front and center in your content? Keep reading, my friend.
Method 2: Modify Your Theme Files
For you code-savvy types out there, displaying random posts directly in your theme files gives you maximum control and flexibility. Here‘s how to do it:
- Fire up your favorite FTP client or use the built-in file manager in your hosting panel.
- Navigate to your WordPress theme folder, usually wp-content/themes/your-theme.
- Decide where you want the random posts to appear. Some common spots are:
- Sidebar (sidebar.php)
- Footer (footer.php)
- After post content (single.php)
- On the homepage (front-page.php or home.php)
- Download a backup of the relevant file before you start tinkering. Safety first! π¦Ί
- Open up the file and find the spot where you want to paste in the following code snippet:
<?php
$args = array(
‘post_type‘ => ‘post‘,
‘posts_per_page‘ => 5,
‘orderby‘ => ‘rand‘
);
$random_posts = new WP_Query( $args );
if ( $random_posts->have_posts() ) {
echo ‘<ul>‘;
while ( $random_posts->have_posts() ) {
$random_posts->the_post();
echo ‘<li><a href="‘ . get_permalink() . ‘">‘ . get_the_title() . ‘</a></li>‘;
}
echo ‘</ul>‘;
wp_reset_postdata();
}
?>- Customize the code to suit your needs:
- Adjust the
posts_per_pagenumber to control how many random posts show up. - Add in
the_post_thumbnail()if you want to include featured images. - Wrap the whole thing in a
<div>with a custom class for styling.
- Adjust the
- Save your changes, upload the edited file back to your server, and give yourself a high five. β
Now when you load up your site, you should see your random posts right where you wanted them:
By weaving the code right into your theme files, you‘ve got complete control over the placement and appearance of your random post sections. Just keep in mind that if you update your theme down the line, you‘ll need to re-add your customizations.
Method 3: Harness the Power of Shortcodes
Wouldn‘t it be great if you could just type a little shortcode and have random posts magically appear? With the free Shortcodes Ultimate plugin, you can!
- Install and activate Shortcodes Ultimate from the WordPress plugin repository.
- Open up the post or page where you want the random posts to go.
- Type in the
[random_posts]shortcode. Boom, that‘s it! π₯
Ok, there‘s a little more to it than that. You can customize your random post display with some handy parameters:
countβ Set the number of posts to show (default is 5)post_typeβ Pull posts from a custom post type (default is ‘post‘)taxonomyandtermsβ Narrow it down to specific categories or tagsexcerpt_lengthβ Include excerpts with a set number of wordsimage_sizeβ Show featured images in a chosen size
For example, this shortcode would display 3 random posts from the "recipes" category with 50-word excerpts and thumbnail-sized images:
[random_posts count="3" taxonomy="category" terms="recipes" excerpt_length="50" image_size="thumbnail"]
Shortcodes Ultimate gives you the power to mix and match parameters to create all kinds of cool random post displays:
[Screenshot of multiple random post sections on a page]And since it‘s a shortcode, you can slide those random posts anywhere you can edit content. Try sprinkling a few in the middle of a long post to entice readers to explore related topics!
Level Up with Advanced Techniques
By now you‘re probably brimming with ideas for using random posts on your site. But wait, there‘s more! Here are some pro tips to take your random displays to the next level:
π² Weighted Randomization: Not all posts are created equal. Give your best content a higher probability of being picked by assigning weights. Here‘s a code snippet to get you started:
$random_posts = get_posts( array(
‘numberposts‘ => 5,
‘orderby‘ => ‘meta_value_num‘,
‘meta_key‘ => ‘weight‘,
‘order‘ => ‘DESC‘
) );This will grab 5 random posts, favoring those with a higher number in the weight custom field. Just add that custom field to your posts and assign weights like 1, 2, 3 to control the odds.
π Mix in Some Curated Picks: Use get_posts() with the post__in parameter to sprinkle in some hand-picked posts along with the random ones. This lets you ensure your most important content gets featured frequently:
$curated_posts = array( 123, 456, 789 );
$random_posts = get_posts( array(
‘numberposts‘ => 5,
‘orderby‘ => ‘rand‘,
‘post__not_in‘ => $curated_posts
) );
$featured_posts = array_merge( $curated_posts, wp_list_pluck( $random_posts, ‘ID‘ ) );This code grabs 5 random posts, excluding the ones you‘ve hand-selected, then merges the two arrays together for a mix of curated and random picks.
π Filter by Custom Fields: If you‘re using custom fields to track data about your posts (e.g. star rating, product category), you can filter your random queries by those fields:
$random_posts = get_posts( array(
‘numberposts‘ => 5,
‘orderby‘ => ‘rand‘,
‘meta_query‘ => array(
array(
‘key‘ => ‘rating‘,
‘value‘ => 4,
‘compare‘ => ‘>=‘
)
)
) );This snippet will pull in 5 random posts with a star rating of 4 or higher. The possibilities are endless β get creative with your custom fields!
Wrapping Up
Phew, that was a whirlwind tour of displaying random posts in WordPress! I hope you‘re buzzing with inspiration to add some serendipity to your own site.
Whether you opt for the simplicity of Recent Posts Widget Extended, the flexibility of customizing your theme files, or the power of Shortcodes Ultimate and get_posts(), the important thing is to experiment and have fun.
Your visitors will thank you for helping them discover hidden treasure in your archives, and you‘ll be rewarded with higher engagement and more appreciative fans. π₯°
So what are you waiting for? Get out there and randomize! And if you found this guide helpful, I‘d love to hear about the creative ways you‘re using random posts on your site. Leave a comment below and let‘s geek out together. π€
Happy WordPressing!
