Support » Plugins » Previous posts link displayed even when there are no previous posts!

  • I’m trying to set the number of posts displayed on an archive page using

    $wp_query->query_vars["posts_per_page"] = 10;

    This is exactly the same as


    except that it doesn’t forget the other variables. query_posts() ignores things like paged, or which category you are browsing, meaning you have to check for them and set them again.

    This works absolutely fine, displaying 10 posts per page, as I want it to, instead of 4, which is what I have in my settings. The problem arises when you get to the last page of posts, i.e. the page containing the earliest posts in the time period or category. There ought not to be a “previous posts” link, since there are none, but when I set posts_per_page manually it is displayed regardless. Clicking on it takes you to an archive page without any posts, with a “previous posts” link too, which leads to yet another page without posts and so on.

    I should point out that this happens regardless of how I set posts_per_page, either by modifying the query array (the first method above) or by using query_posts().

    Has anyone encountered a similar problem, and if so how did they fix it? Or does anyone have any ideas about why this occurs?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I found my solution!

    I noticed that it was only happening when there were 5 posts or more being displayed. 5 is half of 10, so I was able to do this:

    // if the number of posts retrieved is greater than or equal to half the number of posts per page
    if ( $wp_query->post_count >= ( $posts_per_page / 2 ) ) {
    $show_prev_link = true;
    } else {
    $show_prev_link = false;

    And then:

    <div class="navigation top">
    <?php if ($show_prev_link) { ?><div class="previous"><?php next_posts_link('« Previous Entries') ?></div><?php } ?>
    <div class="next"><?php previous_posts_link('Next Entries »') ?></div>

    If since discovered that doesn’t work :S

    The proper way to do it would be to get the number of pages expected for that archive page, and then make sure the previous link isn’t displayed on that last one.

    To do that I need to divide number of posts in category, by the number of posts per page.

    I get the number of posts in the category like so:

    $this_cat = &get_category($wp_query->get_queried_object_id());

    Category post count is in $this_cat->category_count;

    Pseudo PHP:

    number of pages expected = ceil ( category post count / posts per page )

    if ( number of pages expected = current page being viewed ) {
    $show_prev_link = false; }

    Let’s hope that works 🙂

    It does work, but after much experimentation, I found that you have to set posts per page to 1 in Options, and then reset it manually every time you call the Loop.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Previous posts link displayed even when there are no previous posts!’ is closed to new replies.