How to Create a Random Posts Shortcode

In the course of developing your WordPress site, you have likely considered the importance of site navigation. Once users have visited your site, the goal is to provide more opportunities for the user to find other valuable content beyond the page they have landed on. This is why many WordPress themes come with sidebars and widget areas in the header and/or footer to provide more links for users to click on. In the same fashion, you can encourage users to venture further into your site’s posts by providing a list of random posts on a certain page or posts via a random posts shortcode. That may sound complicated, but it’s very simple.

If you have been using WordPress for a while, you have likely used shortcodes with a plugin or theme that allow you to run certain backend functions. If you are not familiar with shortcodes, they are basically short phrases you can add to a page or post to perform a special action.

With a single code snippet added to your theme’s function file, you can easily add a list of random posts to any post or page via a small shortcode. The advantage in this procedure is that you don’t need to install a third party plugin. If you are someone who likes to use your own code, or if you already have too many plugins going, this method is ideal for you.

First, we’ll show you how to create the shortcode, then we’ll show you where to insert it within a page or post.

How to Create Your Random Posts Shortcode

In order to get the most out of this article, we recommend you use a child theme to make these modifications. We are only going to editing the functions file for your child theme. Or, if you are editing your own custom theme, then you are ready to get started now.

Changes to your theme will always be made in “theme directory.” The location of the theme directory is always the same. Starting from the document root of your website, you will go /wp-content/themes/ followed the by the theme you want to edit. For example, if you wanted to edit the Twenty Sixteen theme, the theme directory will be called “twenty-sixten.” Likewise, a Twenty Sixteen child theme will likely be called “twenty-sixteen-child.”

  1. cPanel login screenLog into cPanel
  2. File Manager iconUnder Files choose File Manager
  3. find theme directoryEnter the theme directory (mentioned above)
  4. create functions.php fileCreate a file called functions.php
  5. edit functions.php fileEdit the functions.php file created above
  6. sample functions.php filePaste the following code into the functions.php file:
     <?php 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('random-posts','wpb_rand_posts'); 
    add_filter('widget_text', 'do_shortcode');  ?>
  7. click Save Changes buttonSave the file
  8. Click Close buttonClose the file

Now that we have created the functions file and added our random posts shortcode, we are going to proceed into our WordPress admin area to use the shortcode on a page or post.

How to Use the Random Posts Shortcode in a Page or Post

Now that we have inserted the necessary modifications to allow for a shortcode, we need to use the shortcode somewhere in our site to see how it works.

  1. WordPress login screenLog into your WordPress Dashboard
  2. Add New PostUnder Posts select Add New
  3. Enter a post titleAdd a title to your post
  4. Add shortcode to postIn the main post body, add the following short-code with brackets included: [random-posts]
  5. Click Preview buttonClick the Preview link

Front end preview

You will now see a preview page for your post. In the main body of the post, you will see a list containing five random posts from your site. This means you have successfully completed this tutorial. You can use this shortcode on any page or post.

Well done! You now know how to easily create a random posts shortcode to any page or post in WordPress without plugins.

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+