WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Different number of posts on category page 1

[Resolved] Different number of posts on category page 1

  • I’m using the following code to show the first post of my category template differently to all others posts i.e. the first post only shows a large image whereas all others show a small image and title. The problem is I would like only 5 posts to show on the first page and then 20 on each subsequent page. I’ve hunted around the forums but can’t seem to find anything to suit these specific needs. Any help would be greatly appreciated.

    Here’s my code as it currently stands…

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

Viewing 15 replies - 16 through 30 (of 36 total)
  • It’s a custom theme based on the toolbox theme. Here’s what I use for my cat-news.php category page to display the News category items.

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    Two problems now…

    1. I have the first post styled differently to the other posts, so unsure how to include that in the code you kindly provided.

    2. Just quickly adding your code appears to work but then if I click on the last page in the pagination then I get a page not found.

    Thanks once again for your help with this – I really wouldn’t know where to start with this. I’ve trawled the internet high and low and couldn’t find anything similar.

    Moderator keesiemeijer

    @keesiemeijer

    Your code got modererated can you paste and submit the full code of cat-news.php into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

    Ahh, thanks for the heads up.

    Here’s the code: http://pastebin.com/Ed9yTa7a

    Moderator keesiemeijer

    @keesiemeijer

    Is that the full code?

    Moderator keesiemeijer

    @keesiemeijer

    Maybe this works [untested]: http://pastebin.com/9UVeHvxN

    I have the header: http://pastebin.com/sdvKdrDK

    Then the category content: http://pastebin.com/Ed9yTa7a

    then the footer: http://pastebin.com/jqvyFcyF

    It’s very strange. The page numbers show correctly but clicking on the last page number just takes me to the page but with a Page not found, even though the penultimate page is not the end of the post count i.e. there’s 16 posts missing from the end which should be on the last page?!

    Moderator keesiemeijer

    @keesiemeijer

    Have you tried it with pastebin in my last post?

    Hi. Yep, that’s the one I used i.e. replacing all the content of my cat-news.php file with the code you provided. It’s very odd that the last page number exists but there’s no page to be found even though there should be another 16 posts to display?

    Moderator keesiemeijer

    @keesiemeijer

    On my testserver this works as expected, very strange. I simplified it a bit: http://pastebin.com/hMBkueXp

    Maybe this works.

    Just done a little bit of testing – using your latest code and here’s what’s happening…

    If I edit the following code:

    $paginated_total = 20

    to the same amount that shows in the Admin > Reading > Blog pages show at most total then all works well i.e. 5 posts on first page and 20 on subsequent pages. If, however, I set the code above to something less than is specified in the WP admin then the last page doesn’t work. i.e.

    paginated_total = 20 / reading settings = 20 > Works correctly
    paginated_total = 19 / reading settings = 20 > Page not found
    paginated_total = 21 / reading settings = 20 > Works correctly

    Moderator keesiemeijer

    @keesiemeijer

    Your reading settings shouldn’t influence this. Try changing this:

    $the_query = new WP_Query( $args1 );

    to this:

    wp_reset_postdata();
    $the_query = new WP_Query( $args1 );

    or to this:

    wp_reset_query();
    $the_query = new WP_Query( $args1 );

    Moderator keesiemeijer

    @keesiemeijer

    Also, did you remove the function from your theme’s functions.php. And also look in there if your theme is adding filters to category pages.

    It really is very odd. I’ve checked the functions.php file and there’s nothing in there that would be having any influence on things. Tried those bits of code you mentioned and they all basically worked in the same way but didn’t help with the last page problem.

    Very strange indeed!

    Moderator keesiemeijer

    @keesiemeijer

    Ah, I think I found a solution. put this in your theme’s functions.php and change the category ID in is_category(1) to the (news category) ID where you want 5 posts and 20 posts: http://pastebin.com/SSxWvMnd

    and use this for your cat-news.php: http://pastebin.com/WEjruURY
    This is the only code I added to it:

    <?php
            global $wp_query;
            $first_page_total= 5; // total number of posts on first page
            $paginated_total = 20;
            $found_posts = $wp_query->found_posts;
            $pages = 1 + ceil(($found_posts - $first_page_total) / $paginated_total);
            $wp_query->max_num_pages = $pages;
    ?>

Viewing 15 replies - 16 through 30 (of 36 total)
  • The topic ‘[Resolved] Different number of posts on category page 1’ is closed to new replies.
Skip to toolbar