WordPress.org

Ready to get started?Download WordPress

Forums

Changing posts_per_page for first of paginated pages (5 posts)

  1. aweisman
    Member
    Posted 1 year ago #

    Hello! I'm a little stumped with a pagination issue.

    I've been using the kriesi_pagination() function for all of my pagination and it's worked great. Currently, however, I need to paginate a gallery that will have a certain number of posts on the first page, and a different number on the following pages.

    i.e.

    if($paged > 1){
    	$posts_per_page = 24;
    } else {
    	$posts_per_page = 15;
    }

    And then I would pass these args to WP_Query...

    'posts_per_page' => $posts_per_page,
    'paged' => $paged

    The problem I'm running into is that when I get to the second page, the counts are off. Let's say I have 17 posts. Since the posts_per_page on page 2 is 24, it's not going to start at 16 (showing 16 and 17), it's going to start at 25 (showing nothing).

    I had a look at using offset, but it looks like setting that causes the 'paged' parameter to be ignored (per the codex). I'm not sure how to proceed. The only thing I can think to do now is to work with offset to create my own pagination. I guess I just wanted to check here first to see if anyone had any thoughts on using 'paged' and 'posts_per_page' to get this working, or any tips on other solutions.

    Thanks so much for your time!!

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Is the pagination on a Page template or on a static front page? Or on another template?

    If you use offset you have to provide the pagination:

    For a Page template:
    http://wordpress.org/support/topic/change-posts_per_page-count-on-paginated-pages

    For all other templates:
    http://wordpress.org/support/topic/different-number-of-posts-on-category-page-1

  3. aweisman
    Member
    Posted 1 year ago #

    Thanks so much for the links. Think I got it! Working with offset wasn't really too difficult. I think I was over-thinking it a bit. I was thinking I'd have to ditch my pagination if I couldn't use the paged parameter. But fortunately, the kriesi_pagination() function is using the $paged global which is still available. Anyway, don't want to ramble... Here is what I ended up with for anyone that might need it. And of course feedback is welcome if this could be done better...

    $first_page_post_count = 15;
    $subsequent_pages_post_count = 24;
    $paged = $paged ? $paged : 1;
    
    if($paged > 1){
    	// not first page
    	$posts_per_page = $subsequent_pages_post_count;
    
    	if($paged == 2){
    		// second page
    		$offset = $first_page_post_count;
    	} else {
    		// subsequent pages
    		$offset = $first_page_post_count + ($subsequent_pages_post_count * ($paged - 2));
    	}
    
    } else {
    	// first page
    	$offset = 0;
    	$posts_per_page = $first_page_post_count;
    }

    Then in the query...

    'posts_per_page' => $posts_per_page,
    'offset' => $offset
  4. keesiemeijer
    moderator
    Posted 1 year ago #

    I think you've done brilliantly!

  5. aweisman
    Member
    Posted 1 year ago #

    Sweet! Thanks again :)

Topic Closed

This topic has been closed to new replies.

About this Topic