I am trying to add a button to take the viewer to a random post. Below's the code I am using:-
<?php query_posts(array('orderby' => 'rand', 'posts_per_page' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div class="post-lot">
<a href="<?php the_permalink() ?>">Yes, Robo! Take me there.</a>
</div>
<?php endwhile;
endif; ?>
But, it keeps showing two links. If I change posts_per_page
to 2, it shows 3 links. I tried adding wp_reset_query
but the issue still persists.
query_posts()
until you are 100% sure you have to do so. Useget_posts()
orWP_Query
instead. See the Code Reference.WP_Query
as seen here - wpexplorer.com/random-posts-wordpress but the result's the same