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.”
- Log into cPanel
- Under Files choose File Manager
- Enter the theme directory (mentioned above)
- Create a file called
functions.php
- Edit the
functions.php
file created above - Paste 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'); ?>
- Save the file
- Close 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.
- Log into your WordPress Dashboard
- Under Posts select Add New
- Add a title to your post
- In the main post body, add the following short-code with brackets included: [random-posts]
- Click the Preview link
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.