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:
- Supercharge Your SMS Subscribers With Property Subscription Notification Forms — May 10, 2021
- Easily Embed MFAA and FBAA Calculator on Your Website, or Link to Calculator Modals — September 8, 2022
- Return the Size of an Entire Directory with PHP — May 19, 2017
- Facebook Share Button in WordPress With Shortcode — August 16, 2017
- A Shortcode and Elementor Website Video Slider — January 26, 2023
While any number of posts greater than 1 is returned in a list, a single post is returned as follows:
Random post from July 27, 2021: Mortgage Broker Advertising Compliance
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)