Title: How to display random posts?
Last modified: August 19, 2016

---

# How to display random posts?

 *  Resolved [superoreh](https://wordpress.org/support/users/superoreh/)
 * (@superoreh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/how-to-display-random-posts-1/)
 * I now use this code in excperts on my homepage – it displays date ordered posts.
   How to get posts to display random? – Any idea?
 *     ```
       <?php
       	$post_class = 'odd';
       	$i = 50;
       ?>
       <div id="excerpts" class="section">
   
       <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
   
       	<div class="post <?php echo $post_class; ?>">
   
       		<?php if($i == 1) : ?>
       			<h2 class="section-header"><?php _e('Recent Posts','options'); ?></h2>
       		<?php endif; ?>
   
       		<?php echo get_the_image_link(array('Thumbnail'),'thumbnail'); ?>
   
       		<h3>
       			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
       		</h3>
   
       		<p class="byline">
   
       		</p>
       		<div class="post-meta-data">
       			<p>
       				<span class="categories"><?php _e('Vrsta: ','options'); ?> <?php the_category(', '); ?></span>
       				<br />
       				<span class="tags"><?php _e('','options'); ?> <?php the_tags('', ', ', ''); ?></span>
       			</p>
       		</div>
   
       	</div>
   
       	<?php
       		if('odd' == $post_class) $post_class = 'even';
       		else $post_class = 'odd';
       		$i++;
       	?>
   
       	<?php endwhile; ?>
   
       	<div class="navigation-links section">
       		<?php posts_nav_link('',
       			'<span class="previous">&laquo; '.__('Previous Page','options').'</span>',
       			'<span class="next">'.__('Next Page','options').' &raquo;</span>'
       		); ?>
       	</div>
   
       <?php else: ?>
   
       	<p><?php _e('Sorry, no posts matched your criteria.','options'); ?></p>
   
       <?php endif; wp_reset_query(); ?>
   
       </div>
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/how-to-display-random-posts-1/#post-1018173)
 * Change:
 *     ```
       <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
       ```
   
 * to
 *     ```
       <?php query_posts($query_string . '&orderby=rand'); ?>
       <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
       ```
   
 * Resource:
    [query_posts()](http://codex.wordpress.org/Template_Tags/query_posts)
 *  Thread Starter [superoreh](https://wordpress.org/support/users/superoreh/)
 * (@superoreh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/how-to-display-random-posts-1/#post-1018177)
 * i tried, but hothing has changed.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/how-to-display-random-posts-1/#post-1018182)
 * Worked just fine for me in the index.php file using the WordPress Default Theme.
 * Sure you are putting that into the correct template file?
 * See [Template Hierarchy](http://codex.wordpress.org/Template_Hierarchy)
 *  Thread Starter [superoreh](https://wordpress.org/support/users/superoreh/)
 * (@superoreh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/how-to-display-random-posts-1/#post-1018183)
 * Sorry – my mistake. Yes, it is working perfect. Thanks

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘How to display random posts?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [superoreh](https://wordpress.org/support/users/superoreh/)
 * Last activity: [17 years, 2 months ago](https://wordpress.org/support/topic/how-to-display-random-posts-1/#post-1018183)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
