and like us on FB

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:

While any number of posts greater than 1 is returned in a list, a single post is returned as follows:

Random post from June 6, 2017: Split a String into Two by Character Length with PHP

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.

Random post from August 16, 2017: Facebook Share Button in WordPress With Shortcode

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:

Recommended article from June 21, 2017: Embed Yahoo! News Videos With PHP or WordPress Shortcode

or…

+ Related WordPress Post
May 9, 2017: Simple Cache With PHP

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

For single post, the text is rendered before the data.

author

To return random posts by specific authors, include their IDs.

category

To return random posts by category, include the category ID(s).

tags

To return random posts by tag (as demonstrated above), include the tag ID(s).

description

To include the post excerpt, use description="1"

remove

To remove a specific string of text from the excerpt (handy if you add content to the post), use remove="your text here".

words

The words attribute is the number of words to include in your excerpt.

number

The number of posts to return. If more than one post is required, they’ll be returned in a list.

p

The 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

Default heading style is font-weight: bold;. Alter as required.

datestyle

Default date style is font-size: 0.9em;. Alter as required.

textstyle

Default excerpt text style is font-size: 0.9em;. Alter as required.

cache

To avoid costly requests to the WP database, we cache results for 2 hours by default.

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
Description: Display random posts in WordPress with shortcode.
Download: Shortcode (V0.2) | WP Plugin (V0.2) | | Plugin Page

Shortt URL for this post: http://shor.tt/1QO7