WordPress.org

Ready to get started?Download WordPress

Forums

Change posts_per_page inside loop? (17 posts)

  1. Twansparant
    Member
    Posted 2 years ago #

    Hi there,

    I've got a problem I can't seem to solve...
    On a page displaying posts from a custom post type called 'news-item', I want to change the number of posts displayed on page 2 and further. This because I want the newest (first) post displayed bigger then the rest, but therefore the layout/grid has to change too. So for example:

    On Page 1:
    1 Large post
    4 normal posts
    ---------------
    5 total

    On Page 2:
    6 normal posts
    ---------------
    6 total

    The part that the first post has to be bigger on page 1 works, but I can't seem to change/set/update the posts_per_page parameter inside the loop in order to add it with 1. Is that even possible without making a second wp_query?

    I tried it with:
    set_query_var( 'posts_per_page', 4 );
    and:
    $args -> set( 'posts_per_page', 4 );
    but that's not working and messing up my pagination.

    My code: http://pastebin.com/hR6K4Wtj

    ANY help is welcome!
    Many thanks!

  2. Mitchell Bundy
    Member
    Posted 2 years ago #

    Howdy
    You can't change the query within the loop. I would suggest have a conditional before your arguments manipulating posts_per_page (or $maxposts) depending on what $pageNumber is.

  3. Twansparant
    Member
    Posted 2 years ago #

    Hi there,
    Thanks for your reply! You mean like this:

    if($paged) :
    	$maxposts = 4;
    else:
    	$maxposts = 3;
    endif;

    I tried that allready and it works sort of however somehow post nr. 5 dissapears?

  4. Mitchell Bundy
    Member
    Posted 2 years ago #

    That's because the query thought you were displaying 4 posts on the first page, you'll need to use the offset parameter as well.

  5. Twansparant
    Member
    Posted 2 years ago #

    Thanks for that, I changed it to:

    if($paged) :
    	$maxposts = 4;
    	if ($pageNumber == 2):
    		$offset = 3;
    	endif;
    else:
    	$maxposts = 3;
    endif;

    And that's working! But now the pagination is messed up.
    I have 8 posts in total. On page 1 the wp_paginate shows that there are 3 pages (which is correct 3+4+1=8), but on page 2 it shows there are only 2 pages. When I try to access page/3/ there's no post in it...
    Any thoughts?

  6. Mitchell Bundy
    Member
    Posted 2 years ago #

    Try:

    if($paged) :
    	$maxposts = 4;
    	$offset = 3;
    else:
    	$maxposts = 3;
    endif;

    The offset should be used on every page except the first. Let me know how it works out.

  7. Mitchell Bundy
    Member
    Posted 2 years ago #

    Sorry, this:

    if($paged > 1) :
    	$maxposts = 4;
    	$offset = 3;
    else:
    	$maxposts = 3;
    endif;
  8. Twansparant
    Member
    Posted 2 years ago #

    Nope, same story... Page 3 doesn't show up on page 2.
    But page/3/ shows the same posts as page 2 now.

  9. esmi
    Forum Moderator
    Posted 2 years ago #

    Try using the is_paged conditional

  10. Twansparant
    Member
    Posted 2 years ago #

    Tried it, but same result as:
    if($paged > 1)
    Keep them coming :)

  11. keesiemeijer
    moderator
    Posted 2 years ago #

  12. esmi
    Forum Moderator
    Posted 2 years ago #

    Something like:

    if( !is_paged() ) $maxposts = 3;
    wlse $maxposts = 4;
    $args = array(
    	'posts_per_page' => $maxposts,
    	'post_type' => 'news-item',
    	'paged' => $paged
    );

    should work.

  13. Twansparant
    Member
    Posted 2 years ago #

    Thanks both of you!
    My weekend just started so not able to try it out right now...
    Will try your suggestions as soon as I can!
    Thanks

  14. Twansparant
    Member
    Posted 2 years ago #

    Hi esmi,
    Your solution has the same result as my first attempt: post #5 dissapears...
    I'm gonna try keesiemeijer's suggestion now, looks promising
    Thanks!

  15. Twansparant
    Member
    Posted 2 years ago #

    Hi keesiemeijer,

    Different code, same result unfortunately... But in this case the wp_paginate(); doesn't work anymore. It doesn't show any pagination, when I replace it with:

    <?php next_posts_link('Older Entries »', 0); ?>
    <?php previous_posts_link('« Newer Entries', 0); ?>

    I get the same error as in my code; on page 2 the link to page 3 isn't there... I don't get it anymore.

    My code: http://pastebin.com/SHqS7nFZ

    Any other suggestions?
    Thanks!

  16. Twansparant
    Member
    Posted 2 years ago #

    Found a solution/workaround!
    I modified my code a bit in order to let the wp_paginate() function show the right amount of pages. The function can pass the total amount of pages and the current page as variables. However, if you pass 'pages' the highlighting of the current page stops working so you have to pass 'page' as well.

    My final code: http://pastebin.com/51r7PTwb

  17. Yonyoh
    Member
    Posted 2 years ago #

    thanks for sharing.

Topic Closed

This topic has been closed to new replies.

About this Topic