WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Hack number of posts on category (4 posts)

  1. asamuels
    Member
    Posted 6 years ago #

    I have read many posts and found plugins to show all posts on a category page, but i am looking at doing the following scenario and wondered if anyone know a code hack to get the desired result?

    I will be setting up a custom category page which will pull say 3 of the most recent posts in that category and a specific post as a featured post for example.

    I would then like to link to category/page/2 from the category and page 2 will display all posts.

    I tried using the following in the category template, but it doesnt seem to work:

    <?php if( is_category('1') & ($paged = 2) ) query_posts($query_string . "&posts_per_page=500"); ?>

    Does anyone have any ideas please?

  2. Austin Matzko
    Member
    Posted 6 years ago #

    You need something like the following in your category.php template:

    if( is_category('1') & (2 == $paged) ) {
            $GLOBALS['wp_query']->query_vars['posts_per_page'] = 500;
            $GLOBALS['wp_query']->query_vars['paged'] = 1;
            $GLOBALS['wp_query']->query_vars['offset'] = 3;
            query_posts($GLOBALS['wp_query']->query_vars);
    }

    Here's why what you have doesn't work: for one thing, you're setting $paged to 2 when you have $paged = 2.

    For another, when you say that posts_per_page is 500, the query will assume that the first page had 500 also, and so show only the next 500 posts from category #1.

    So what you really want to use is the offset query variable, and query as though it were the first page but offset by as many posts as you have on the real first page.

  3. asamuels
    Member
    Posted 6 years ago #

    That worked an absolute treat. Thanks so much for you time on this!!

  4. matthias_with_style
    Member
    Posted 6 years ago #

    unline asamuels this is the first successful find for me regarding this subject. Thanks for your solution, filosofo. I wanted the simpler version, the existing posts_per_page value (from the options) on the index page, but in a specific category I wanted all posts.

    In order to apply your solution to my code, I need to set a very high number, such as:

    $GLOBALS['wp_query']->query_vars['posts_per_page'] = 1000;
    query_posts($GLOBALS['wp_query']->query_vars);
    //now we're going into the loop

    using -1 or '-1' returns nothing. Not a problem for me this time, but is there an explanation for this, for posteriority? And could this be solved using filters/actions instead of putting the code into the template?

Topic Closed

This topic has been closed to new replies.

About this Topic