Forums

[resolved] Navigation broken when using query_posts and posts_per_page (7 posts)

  1. lmatteis
    Member
    Posted 3 weeks ago #

    Hello,
    I currently have a navigation setup outside my main loop which simply uses the posts_nav_link() function.

    My main loop is controlled by the query_posts function and looks something like this:

    $query = array();
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $query['cat'] = get_query_var('cat');
    $query['posts_per_page'] = 7;
    $query['paged'] = $paged;
    
    query_posts($query);
    
    if(have_posts()) :
        while(have_posts()) :
    
        the_post();

    The problem is that the navigation doesn't recognize the limit imposed by me in query_posts (7), rather it works with the limit set in the Wordpress Reading settings page, which is 10 by default.

    This screws up my navigation, and makes it impossible to use. Maybe I need to overwrite the Reading settings page limit somehow, but haven't been able to find anything.

  2. songdogtech
    Member
    Posted 3 weeks ago #

    Wondering if you need rewind_posts() to reset the loop? rewind_posts >> The Loop « WordPress Codex

  3. lmatteis
    Member
    Posted 3 weeks ago #

    @songdogtech:

    Unfortunately this won't help as this is the only Loop I have in my theme. Maybe you can reproduce this problem as well, just add query_posts("paged=$paged&posts_per_page=2") to your Loop, and set the Page Limit in the wpadmin settings to like 10. You'll see that your nav will not behave normally.

    Maybe this is a bug?

  4. songdogtech
    Member
    Posted 3 weeks ago #

    This loop below works fine for me; maybe the extra parameters you are using are throwing the query off?

    <?php query_posts("paged=$paged&posts_per_page=2&cat=4"); if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>
    <?php else : ?>
    <?php endif; ?>
  5. lmatteis
    Member
    Posted 3 weeks ago #

    <?php posts_nav_link(); ?>
    
    <?php query_posts("paged=$paged&posts_per_page=2&cat=4"); if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>
    <?php else : ?>
    <?php endif; ?>

    You are missing the posts_nav_link() function, does the nav work fine with that code?

  6. songdogtech
    Member
    Posted 3 weeks ago #

    Yes; but my posts_nav_link() is after endif()

  7. lmatteis
    Member
    Posted 3 weeks ago #

    Oh interesting, so the posts_nav_link must go after the query_posts. Thanks a lot!

Reply

You must log in to post.

About this Topic