Forums

[resolved] showposts breaks my archives - fix? (3 posts)

  1. tdellaringa
    Member
    Posted 4 years ago #

    I wanted to show only 1 post on my blog home page, but 10 on my archive/category pages. It was suggested I leave the options set to show 1 post, then add the following to my archive:

    <?php query_posts('showposts=10'); ?> <-- added this line
    <?php while (have_posts()) : the_post(); ?>

    This initially seemed to work, until I realized I was getting *all* of my posts on every archive (month) and category page. Removing the line fixes it back.

    Why does showposts pull *all* posts? In any event, is there another way I can accomplish showing multiple posts on my archive/category pages but still only 1 on the home blog page? I'm not using, nor do I want to use, a static home page.

    Thanks

  2. Otto
    Tech Ninja
    Posted 4 years ago #

    The reason it does this is because you're overriding the default query string parameters.

    See, when WordPress starts up, it gets the URL/permalink and automatically figures out what the viewer is asking for. Archives, categories, etc. These become the "query string" and get passed to "query_posts", which fetches the posts.

    However, you're manually calling query_posts and giving it your own query_string. This changes what posts you get back. In order to keep the original settings, and only override the "showposts" part of the query string, you need to add to the existing query string instead of overwriting it entirely.

    And you do that like this:
    query_posts($query_string . "&showposts=10");

    Simple, huh? More examples and info here:
    http://codex.wordpress.org/Template_Tags/query_posts

    Read the whole thing, it's worth it to understand what's going on.

  3. tdellaringa
    Member
    Posted 4 years ago #

    Thanks Otto, that does indeed work. Heading over right now to read that page :)

Topic Closed

This topic has been closed to new replies.

About this Topic