• 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: https://pastebin.com/hR6K4Wtj

    ANY help is welcome!
    Many thanks!

Viewing 15 replies - 1 through 15 (of 16 total)
  • 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.

    Thread Starter Twansparant

    (@twansparant)

    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?

    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.

    Thread Starter Twansparant

    (@twansparant)

    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?

    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.

    Sorry, this:

    if($paged > 1) :
    	$maxposts = 4;
    	$offset = 3;
    else:
    	$maxposts = 3;
    endif;
    Thread Starter Twansparant

    (@twansparant)

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

    Try using the is_paged conditional

    Thread Starter Twansparant

    (@twansparant)

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

    Moderator keesiemeijer

    (@keesiemeijer)

    Something like:

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

    should work.

    Thread Starter Twansparant

    (@twansparant)

    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

    Thread Starter Twansparant

    (@twansparant)

    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!

    Thread Starter Twansparant

    (@twansparant)

    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: https://pastebin.com/SHqS7nFZ

    Any other suggestions?
    Thanks!

    Thread Starter Twansparant

    (@twansparant)

    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: https://pastebin.com/51r7PTwb

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Change posts_per_page inside loop?’ is closed to new replies.