How to Display Random Posts in WordPress

Introduction to Random Posts in WordPress

WordPress is one of the most popular platforms for creating websites and blogs. One of the features that many administrators want to implement is the ability to display random publications on their sites. This not only improves the user experience, but can also increase the time spent on the page and encourage content exploration. In this article, we will explore various ways to achieve this in WordPress.

Methods for Displaying Random Posts

There are several ways to display random posts in WordPress. Here are some of the most popular methods.

Using WordPress Widgets

One of the easiest ways to display random posts is by using the widget. Recent Entries offered by WordPress. However, this method only displays recent posts, so to display random content, you need to use a plugin or custom code.

Implementing Specific Plugins

There are numerous plugins available that make it easy to display random posts. Some of the most recommended ones are:

  • Random Post WidgetThis plugin allows you to display random posts in a widget, which is ideal for sidebars or footers.
  • WP Random PostsWith this plugin, you can insert random posts anywhere on your site using a shortcode.
  • Related Posts ThumbnailsAlthough it is designed to display related posts, it also offers the option to display random posts with an attractive visual aesthetic.

Custom Code to Display Random Posts

If you prefer not to use plugins, you can also implement custom code in your WordPress theme. Here is an example of how to do this.

<?php
// Consulta para obtener publicaciones aleatorias
$args = array(
    'orderby' => 'rand', 'posts_per_page' =&gt; 5 // Change the number as you prefer );
$random_posts = new WP_Query($args); if ($random_posts-&gt;have_posts()) : while ($random_posts-&gt;have_posts()) : $random_posts-&gt;the_post(); echo '<a href="/en/' . get_permalink() . '/">'. get_the_title() .		
How to Display Random Posts in WordPress
Download our web maintenance guide Free of charge!
Free guide for freelancers and small businesses that want to avoid surprises and improve their web performance.
'</a><br>'; endwhile; wp_reset_postdata(); endif; ?&gt;

This code creates a query that randomly selects 5 posts and displays their titles as links. You can insert this code into the file functions.php of your topic or in a shortcode to use anywhere on your site.

User Experience Optimization

Displaying random posts can be very efficient, but it is essential to ensure that the user experience is not affected. Here are some tips for optimizing this feature:

  • Limit the number of postsDo not display too many random posts at once, as this may overwhelm the user.
  • Include featured images: Posts with images are more appealing. Ensure that random posts include featured images.
  • Filter by categoriesIf you want random posts to be relevant, consider limiting them to certain categories.

Example of Implementation in a Theme

If you decide to implement the code in your theme, you can customize it further. For example, you can add featured images to random posts as follows:

'rand',
    'posts_per_page' =&gt; 5
);
$random_posts = new WP_Query($args);
if ($random_posts-&gt;have_posts()) :
    while ($random_posts-&gt;have_posts())) : $random_posts-&gt;the_post();
        if (has_post_thumbnail()) {
            echo '<a href="/en/&apos; . get_permalink() . &apos;/">' . get_the_post_thumbnail() . '</a>';
        }
        echo '<a href="/en/&apos; . get_permalink() . &apos;/">' . get_the_title() . '</a><br>';
    endwhile;
    wp_reset_postdata();
endif;
?&gt;

This code checks whether the post has a featured image and displays it next to the title, thus providing a more attractive presentation.

Conclusions on Random Publications

Incorporating random posts into your WordPress site is an effective strategy for keeping visitors engaged and encouraging interaction with your content. Whether through the use of plugins, widgets, or custom code, there are multiple ways to achieve this. The key is to choose the method that best suits your needs and optimize the presentation to enhance the user experience.