Generating a Random Post in WordPress
There are many plugins out there that an generate a random post to be inserted into your website. An example of such a random post is shown to the right of this entry in the box-set off. Some of the plugins come with their own shortcodes. But there’s no need to install another plugin if you have Shortcodes Ultimate installed.
Shortcodes Ultimate by Vladimir Anokhin is a popular plugin for WordPress supplying numerous helpful shortcodes to add various features to your posts.
Using Shortcodes Ultimate to Generate a Random Post
This method permits you to customize the random listing so that it can include only the title, the title and the featured image, or even the entire content.
- Install the Shortcodes Ultimate plugin if you haven’t already.
- On your server, in the “wp-content/plugins/shortcodes-ultimate/” folder, create a new file called “random-post.php”. You can do this through the file manager on CPanel or other frontend on your host (or through SSH if you have command line access).
- Edit the “random-post.php” to add code for your random post – use (and modify if you wish) from one of the following:
Template Code to Show Random WordPress Post Showing Just the Title and Featured Image:
123456789101112131415161718192021222324252627282930313233343536373839404142434445<div class="su-posts su-posts-teaser-loop"<?php$current_post_id = get_the_ID();$posts = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1', 'post__not_in'=>array($current_post_id)));if ( $posts->have_posts() ) {while ( $posts->have_posts() ) :$posts->the_post();global $post;?><hr><?phpif (strlen($truncated_content) >= 500) {$truncated_content = substr($truncated_content, 0, strrpos(substr($truncated_content, 0, 500), ' '));}?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p><a href="<?php the_permalink(); ?>"><img src="<?php echo get_the_post_thumbnail_url(); ?>"></img></a></p><?phpendwhile;}// Posts not foundelse {echo '<h4>' . __( 'Posts not found', 'shortcodes-ultimate' ) . '</h4>';}?></div>Template Code for Random Post To Show Full Content
12345678910111213141516171819202122232425262728293031<div class="su-posts su-posts-teaser-loop"><?php// Posts are found$posts = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );if ( $posts->have_posts() ) {?><ul style="list-style-type:disc;"> <?phpwhile ( $posts->have_posts() ) :$posts->the_post();global $post;?><li id="su-post-<?php the_ID(); ?>" class="" style="line-height: 14px; margin-right: auto; margin-left: 10px; text-align: left"><p class=""> • <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p></li><?phpendwhile;?></ul><?php}// Posts not foundelse {echo '<h4>' . __( 'Posts not found', 'shortcodes-ultimate' ) . '</h4>';}?></div>Random Template Code To Show The First Few Paragraphs of WordPress Post
NOTE: This option is not recommended as it will arbitrarily truncate your post content including HTML tags which may mess up the formatting on the rest of the page where this code is inserted) .
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273<div class="su-posts su-posts-teaser-loop"><?php// Posts are found$current_post_id = get_the_ID();$posts = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1', 'post__not_in'=>array($current_post_id)));if ( $posts->have_posts() ) {while ( $posts->have_posts() ) :$posts->the_post();global $post;?><hr><?php$truncated_content = get_the_content();//Watch that this arbitrary truncation doesn't cause havoc with rest of html code. Not recommended !if (strlen($truncated_content) >= 500) {$truncated_content = substr($truncated_content, 0, strrpos(substr($truncated_content, 0, 500), ' '));}?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p><?php echo $truncated_content; ?></div><?phpendwhile;}// Posts not foundelse {echo '<h4>' . __( 'Posts not found', 'shortcodes-ultimate' ) . '</h4>';}?></div> - After pasting in one of the three above, save the file.
Customize Your Shortcodes Ultimate Template
Grab This Code for Your Shortcodes Ultimate Template
Try Out The Random Post Short Code
- Click on the Shortcodes Ultimate button in your editor:
- A dialog will open. Click on the “Posts” code:
- Edit the Template name to read “templates/random-post.php” to match the name of the template you just created.
- Complete the rest of the dialog options. When finished, click “Insert” to insert the Posts short code.
- Preview the post to verify that a random post was inserted.