Not unlike our articles on rendering a list of future WP posts, and another on listing child pages, the shortcode function on this page utilizes WP's wp_query() class to display a list of random posts. Not entirely unlike a scheduled post on displaying a reading list, displaying random posts is another means of converting your readership by displaying additional article content. Additionally, providing access to older content showcases valuable content that would otherwise be buried in obscurity.
The Result
To display 5 random posts (the default number) we'll use the shortcode of [random]
. The result is as follows:
- Belief’s BeLearn Module Provides a REAL Understanding of Digital Marketing — April 29, 2021
- Display Flight Radar Plots on Your Website With WordPress Shortcode or PHP — June 11, 2017
- Using AI To Write Finance Blog Posts and Website Content — December 11, 2022
- Create A LinkedIn Company Follow Button With WordPress Shortcode — January 27, 2018
- Is Air NZ’s Safety in Paradise Video Sexist? — February 4, 2014
While any number of posts greater than 1 is returned in a list, a single post is returned as follows:
Random post from April 20, 2021: Why We’ve Archived the Banking Royal Commission Data
The purpose of removed formatting is so that the content can be used in one of our text boxes. A sample is as follows:
The textboxes work well in company with single posts because you can return posts relevant to a specific category or tag. For example, to display a random YouTube post (in a YouTube textbox), usage of [bmtextbox type="youtube"][random number="1" tag="69"][/bmtextbox]
returns the following (with 69 being the tag ID for 'youtube').
Another example: A random Facebook post.
You might choose to use a random post as a means to suggest a 'recommended' post. Again, by tag, it's likely to be relevant. Shortcode of [bmtextbox type="wordpress"][random number="1" tag="66" text="Recommended article from"][/bmtextbox]
returns:
or...
An example list of random posts with excerpts can be found here.
You may optionally return a post by category or author, and you may also include the description (details below).
WordPress Shortcode
Copy and paste the WordPress function into your theme's functions.php
file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.
If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.
Shortcode Attributes
The following list of attributes will alter the behaviour of your list.
text
text
is rendered before the data.author
category
tags
description
description="1"
remove
remove="your text here"
.words
words
attribute is the number of words to include in your excerpt.number
number
of posts to return. If more than one post is required, they'll be returned in a list.p
p
attribute is for single random posts. If you're including the text inline, use default (false). Otherwise, use p="1"
. This overcomes issues with shortcodes and paragraph breaks.headingstyle
font-weight: bold;
. Alter as required.datestyle
font-size: 0.9em;
. Alter as required.textstyle
font-size: 0.9em;
. Alter as required.cache
Considerations
- The function can return posts of any type in an order. However, for the purpose of simplicity we make shortcodes available for each task.
Download
Recommended: Text Boxes With WordPress Shortcode.
Title: BeliefMedia Random Posts (WP Plugin)
Description: Display random posts in WordPress with shortcode.
Download • Version 0.2, 2.1K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (1.4K)