WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Different number of posts on category page 1 (37 posts)

  1. greencode
    Member
    Posted 2 years ago #

    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]

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    This code will only load 5 posts on the first category page and 20 on subsequent pages. Put this in your theme's functions.php: http://pastebin.com/izVjUE5e

  3. greencode
    Member
    Posted 2 years ago #

    Thanks for this. I added that to my functions and I got the following error when viewing the page:

    Fatal error: Call to undefined method WP_Query::is_main_query() in /home/mysite/public_html/dev/wp-content/themes/mytheme/functions.php on line 215

    215 appears to be this line:

    if (!is_admin() && $query->is_main_query()){

    Any ideas?

  4. greencode
    Member
    Posted 2 years ago #

    If I remove the code I mentioned in the my previous post then all works well but I'm presuming that's there for a reason.

  5. keesiemeijer
    moderator
    Posted 2 years ago #

    What version of WordPress are you using?

  6. greencode
    Member
    Posted 2 years ago #

    3.2.1

  7. keesiemeijer
    moderator
    Posted 2 years ago #

    the function is_main_query() was introduced in WordPress 3.3.0 that's why it doesn't work. Why don't you update to the latest version?

  8. greencode
    Member
    Posted 2 years ago #

    I will, but there's a number of plugins that I need to ensure will work on the current version of WP.

    Is it okay to use that code without the the main_query part of it?

    As I mentioned, it seems to work okay without it but then again I've only checked a page or two.

  9. keesiemeijer
    moderator
    Posted 2 years ago #

    Is it okay to use that code without the the main_query part of it?

    No.

    This will work with older WordPress versions:
    change this:

    if (!is_admin() && $query->is_main_query()){

    to this:

    global $wp_the_query;
    if (!is_admin() && ($wp_the_query === $query )){
  10. greencode
    Member
    Posted 2 years ago #

    Thanks again for this. The only trouble with this now is that the pagination is incorrect. For example I only have 6 pages for the category but on the first page it's showing that I have 20.

    I'm using the following code at the bottom of my page, if that's any help:

    <?php /* Display navigation to next/previous pages when applicable */ ?>
    <?php if (  $wp_query->max_num_pages > 1 ) : ?>
    <nav id="nav-below">
    <?php wp_pagenavi(); ?>
    </nav><!-- #nav-below -->
    <?php endif; ?>
  11. keesiemeijer
    moderator
    Posted 2 years ago #

    Yeah I just realized that it doesn't work with wp_pagenavi. I have to figure that one out.

  12. greencode
    Member
    Posted 2 years ago #

    Thanks, really appreciate your help with this.

  13. greencode
    Member
    Posted 2 years ago #

    Just wondering if this is of any help to resolve this issue: http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html

  14. keesiemeijer
    moderator
    Posted 2 years ago #

  15. greencode
    Member
    Posted 2 years ago #

    Forgive me. I'm a little lost with what solution on that page I am looking at? Do I still keep the function in place from above?

  16. keesiemeijer
    moderator
    Posted 2 years ago #

    Do I still keep the function in place from above?

    no, remove that function. and use something like this in your theme's category.php: http://pastebin.com/EsfqXTYz

    What theme do you use?

  17. greencode
    Member
    Posted 2 years ago #

    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.

  18. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  19. greencode
    Member
    Posted 2 years ago #

    Ahh, thanks for the heads up.

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

  20. keesiemeijer
    moderator
    Posted 2 years ago #

    Is that the full code?

  21. keesiemeijer
    moderator
    Posted 2 years ago #

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

  22. greencode
    Member
    Posted 2 years ago #

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

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

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

  23. greencode
    Member
    Posted 2 years ago #

    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?!

  24. keesiemeijer
    moderator
    Posted 2 years ago #

    Have you tried it with pastebin in my last post?

  25. greencode
    Member
    Posted 2 years ago #

    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?

  26. keesiemeijer
    moderator
    Posted 2 years ago #

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

    Maybe this works.

  27. greencode
    Member
    Posted 2 years ago #

    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

  28. keesiemeijer
    moderator
    Posted 2 years ago #

    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 );
  29. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  30. greencode
    Member
    Posted 2 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic