118 Views

Displaying Random Posts in WordPress

Displaying random posts in your wordpress site is easier than you think. With random posts, your users a chance to discover variety of your content.
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, if you have been running your site for long time, new users will not be able to see your older articles. One way to allow users to discover your old content is by making internal linking habit. Linking to your older posts will help user discover them and it will also increase your pageviews and inprove SEO score.
Another way to let your users discover your posts is by displaying random posts in your sidebar. And there are more than one ways to do it.
The easiest way to display random posts is by using a plugin. It is recommended for most wordpress users. First thing you need to do is install and activate the Advanced Random Posts Widget plugin by Them Junkie.

After the plugin is activated, visit Appearance » Widgets page and there. You will notice a new widget labeled ‘Random Posts’ under the list of available widget.

wp-random-posts-widget.png

To get random posts you need to add this widget to a sidebar where you want to display your random posts.
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.

random-posts-widget-settings
Advanced Random Posts Widget is a powerful plugin with lots of customization options where, 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.
If you are an experienced user, this plugin also allows you to add custom before and after HTML, and your own custom CSS as well.
Remember to click on the save button to store your widget settings. You can now visit your website to see random posts widget in action.

Another, more advanced method of displaying random posts is by using code.
For this method, you have to add code to your wordpress theme files.
First, add this code in your theme’s functions.php file or a site-specific plugin (if you have one). For the purpose of this tutorial, add this code in your theme’s functions.php file.

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');

What this code does is, it simply creates a function that displays 5 random posts, and 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].

Related Posts

%d bloggers like this: