WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] My pagination doesn't work with query_posts() (5 posts)

  1. Ahrengot
    Member
    Posted 3 years ago #

    I have 2 "blog" pages on my new site. The home page itself and a page titled blog with the following code:

    <?php
    	// Query posts
    	query_posts("posts_per_page=5");
    ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<!-- all the loop stuff -->
    <?php endwhile; endif; ?>

    So what I do to overwrite the default page behavior, as you can see, is call query_posts() without including the $query_string variable.

    However this screws up my pagination some how. When I call the second page it simply fetches the same posts over and over again. You can try this by going to http://ahrengot.com/blog/ and then navigating to http://ahrengot.com/blog/page/2 or by clicking the "show more posts" button which does the same thing, but loads the page via ajax (Using Paul Irish' Infinite Scroll plugin to do this).

    I figure that I'm going at this from a wrong angle, so I would love any pointers in the right direction!

    Many thanks in advance :)

  2. Ahrengot
    Member
    Posted 3 years ago #

    My temporary fix is setting post_per_page to -1 so my query now looks like this

    <?php
    	// Query ALL posts
    	query_posts("posts_per_page=-1");
    ?>

    However, this is just a temporary fix. I would much prefer to have pagination enabled on my blog page, so any help is still greatly appreciated.

  3. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    global $query_string; query_posts($query_string . "&posts_per_page=5");

    http://codex.wordpress.org/Function_Reference/query_posts#Usage_Note

    alternative:
    http://codex.wordpress.org/Function_Reference/WP_Query#Pagination_Parameters

    query_posts("posts_per_page=5&paged=" . get_query_var('paged'));

  4. Ahrengot
    Member
    Posted 3 years ago #

    Perfect — Many thanks, alchymyth!

  5. radiofranky
    Member
    Posted 2 years ago #

    Hi,
    I'm trying to display the posts of the same category of the current post and paginated. However, no matter what i do, no pagination is showing.

    this is the loop I'm using

    <?php query_posts('category_name='. $category->category_nicename .'&posts_per_page=5'.'&paged='.$paged);
    
    ?>
    	 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php endwhile; endif; ?>

    thanks

Topic Closed

This topic has been closed to new replies.

About this Topic