I have a page on wordpress where i want to display 10 random posts. It is working using that code:
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */
query_posts( 'orderby=rand&posts_per_page=10' );
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
[...]
I want to disable pagination on that page, if i disable it using nopaging=true, all posts will be displayed. How to set a limit while limiting the number of posts?
first of all, you should put the call to
query_postsoutside the if statement forhave_posts.In short, you’ve essentially already ‘disabled’ pagination with the query_posts call. You simply haven’t removed the links (the
twentyeleven_content_navs), that allow users to ‘paginate’. In fact, the users wouldn’t be changes pages at all really. Let me explain…When the page loads, 10 posts are selected at random. At the moment, links display (beause WordPress knows that there are more than 10 posts that could be shown, and as far as I’m aware there is not a way of telling it otherwise, unless you use
get_postsinstead).Anyway, if a user clicks to the next page, the page loads again – but in fact you query over-rides their ‘page-2’ request, and shows the first 10 random posts. But these posts won’t be the same as the original, as it’s random.
In short, the pagination links are, in effect, simply refreshing the page, revealing 10 new random posts: despite the fact the url suggests they are navigating through pages.
In summary, just remove the links!