How to Show Random Posts In WordPress

Table of Contents

As you may have noticed at the bottom of my theme pages I show 3 “Random” themes that will switch every time the page is refreshed.

This allows me not only to show you a few themes you may not have seeing before, but it also helps spread out links across the site. When a search engine crawls the site they will see different links on each page as opposed to having the same “newest” or “popular” posts across your whole site.

The method to Show Random Posts is actually very easy and can be achieved with a simple call to the database. Plus, there are a few wordpress template tags that you can use for various effects.

There are two ways to display random related posts: with code or with a plugin.

Display Random Posts With Code

To show a list of random posts you can insert the following code wherever you want them displayed and then tweak accordingly.

<?php  // Query random posts  $the_query = new WP_Query( array(  	'post_type'      => 'post',  	'orderby'        => 'rand',  	'posts_per_page' => 3,  ) ); ?>    <?php  // If we have posts lets show them  if ( $the_query->have_posts() ) : ?>    	<div id="randomposts">  		<h3><?php _e( 'Random Posts', 'text_domain' ); ?></h3>  		<ul>  			<?php  			// Loop through the posts  			while ( $the_query->have_posts() ) : $the_query->the_post();  ?>  				<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>  			<?php endwhile; ?>  			<?php wp_reset_postdata(); ?>  		</ul>  	</div>    <?php endif; ?>


As  you see in the “Full Code”  I have gone ahead and added a whole div container for the random posts as well as an h3 title tag. This way you can go into your stylesheet and make some changes so that you can display them the way you want.

Head over to the WordPress Codex to learn more about the WP_Query() class so you can alter the arguments to better suit you needs.

Display Related Posts With A Plugin

If you don’t want to to use code to add random related posts you can use a plugin. One we’d recommend is the free Yet Another Related Posts Plugin.

YARPP Plugin

With YARPP you can display various related posts, pages and custom post types for your content. This way each post and page displays a unique combination of related content. You can also upgrade to a Pro version of the plugin is you want to include sponsored or features content.

Whichever you choose, adding related posts is a great idea for your readers and for your SEO. We hope you give one of our methods a try!

Source: WP Explorer

Follow Us On:

Copyright Notices: This website is not sharing themes by cloning or cracking. We respect the GNU General Public License (GPL) and the hard work of the theme creator. On this website, we share that link those are already stored somewhere else on the internet and are not a part of this website. Our website does not carry any responsibility for them. If your copyrighted material has been indexed by our site and you want this material to be removed then contact us immediately. We will remove it in 48 hours. All items contained within the directory on “DirectWordpressNulled” are developed by third-party developers and redistributed by “DirectWordpressNulled” under the terms of the General Public Licence (GPL).

Lastest Blog & Tips:

Table of Contents

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock