Categories: Wordpress

Add Random Post with Shortcodes Ultimate

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.

  1. Install the Shortcodes Ultimate plugin if you haven’t already.
  2. Customize Your Shortcodes Ultimate Template

  3. 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).
  4. Grab This Code for Your Shortcodes Ultimate Template

  5. 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:

    
    <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><?php
    
                    
                    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>
        <a href="<?php the_permalink(); ?>"><img src="<?php echo get_the_post_thumbnail_url(); ?>"></img></a></p><?php
    
    
    
       endwhile;
        
      }
      // Posts not found
      else {
       echo '<h4>' . __( 'Posts not found', 'shortcodes-ultimate' ) . '</h4>';
      }
    
     ?>
    </div>

    Template Code for Random Post To Show Full Content

    
    
    
    
    'rand', 'posts_per_page' => '1' ) ); if ( $posts->have_posts() ) { ?>
        have_posts() ) : $posts->the_post(); global $post; ?>
    ' . __( 'Posts not found', 'shortcodes-ultimate' ) . ''; } ?>

    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) .
    '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; ?>
    = 500) { $truncated_content = substr($truncated_content, 0, strrpos(substr($truncated_content, 0, 500), ' ')); } ?>
    ' . __( 'Posts not found', 'shortcodes-ultimate' ) . ''; } ?>
  6. After pasting in one of the three above, save the file.

Try Out The Random Post Short Code

  1. Click on the Shortcodes Ultimate button in your editor:

  2. A dialog will open. Click on the “Posts” code:

  3. Edit the Template name to read “templates/random-post.php” to match the name of the template you just created.
  4. Complete the rest of the dialog options. When finished, click “Insert” to insert the Posts short code.
  5. Preview the post to verify that a random post was inserted.
Charlie

Share
Published by
Charlie

Recent Posts

KeePassXC Password Manager – Use Auto-Type Instead of the KeePassXC Plugin

Avoiding Browser Plugins with KeePassXC KeePassXC is a popular free open source password manager. As…

3 years ago

A Simple Step to Delete Thousands of Useless Gmail Messages and Free Up Space

If you've had a Gmail account for years, like I have, at some point you…

3 years ago

Back Up Your Raspberry Pi Virtually

Here's a little trick I use to back up my Raspberry Pi without having to…

4 years ago

Unclutter Your GMail Without Missing Out On Your Newsletters

I try to keep my email inbox fairly clean but I do subscribe to some…

4 years ago

Windows Reinstall: Use Secret Migration Tool to Restore All Settings Even If Option Disabled; Plus Tips to Restore Programs After a Clean Install

I was recently faced with the dreaded prospect of re-installing Windows 10. While the standard…

4 years ago

Pi-hole & Unlimited Whitelist for OpenDNS, Cleanbrowsing, Etc.

Pi-Hole and Upstream DNS Providers Pi-hole is an excellent whole-home, self-hosted ad blocker and DNS…

4 years ago