Solved: Random Post List with Thumbnail without a Plugin in WordPress

WordPress is used by more than 60 million websites, including 33.6% of the top 10 million websites WordPress is one of the most popular content management system solutions in use. WordPress has also been used for other application domains such as pervasive display systems (PDS).

It also has a lots of plugin to customize our needs but sometimes these plugins not satisfy as per the marks.

One such problem is Random Post List with thumbnail… even after a lot of searching I couldn’t find a better solution than write my own code…

Check out the below video… and watch step by step instruction how to add Random Post List to any specific post or page.

Subscribe to Desktop Publishing Tips

 

Below is the code I have used for Random Post List with thumbnail

<!-- Random Post link code begins  -->
<?php
 if (is_page ('231')) { 
// Query random posts
$the_query = new WP_Query( array(
	'post_type'      => 'post',
	'orderby'        => 'rand',
	'posts_per_page' => 8,
) ); ?>

<?php
// If we have posts lets show them
if ( $the_query->have_posts() ) : ?>

	<div id="randomposts">
		<h2><?php _e( 'In the mean time check out these related macros and scripts.. 🙂', 'text_domain' ); ?></h2>
		<ul>
			<?php
			// Loop through the posts
			while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
				<h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
		<?php		
				if ( has_post_thumbnail() ) {
    echo '<a href="' . get_permalink($post->ID) . '" >';
    the_post_thumbnail('medium');
    echo '</a>';
} else {
    echo '<img src="';
    echo get_bloginfo('template_directory');
    echo '/img/fallback-featured-image.jpg" />';
}
	?>			
<hr />
			<?php endwhile; ?>
			<?php wp_reset_postdata(); ?>
		</ul>
	</div>

<?php endif; 
} ?>
 
<!-- Ranodm post link ends -->

In this code you have to replace your page id with the page where you want to use this script in line 3.

If you want to use it on a multi page then you can replace that line with the following code:

if( is_page( array( 11, 22, 33, 44 ) ) ) {

where number are you sites page id…

To show it on all pages just depreciate the if condition.

I have applied this code at this link below… click to have a live show 🙂

(Visited 8,379 times, 1 visits today)

Rakesh Bhardwaj

A professional Graphic Design, working in a multi-national company from past six years.

Learn More →

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.