Pagination with different number of items on page 1

A simple scenario: I’m paginating a bunch of posts—8 posts per page—but I just want the first 7 on page 1.

I know I can manually display the first 7 posts then display the rest using ->offset(), but then I won’t be able to use my usual pagination snippet.

Right now I’m appending an empty post to the top of my collection, paginating it, then removing the first post. It works fine, but it seems like a kludge in my controller.

Is there a better way to achieve this?

The other day i wanted some code only on the first page of the paginiation and not on any other page. Maybe it will help you too…

$pager = param('page') ? 'allotherpages' : 'firstpage';
echo $pager;

if its the fist page of the pagination, $pager will be set to ‘firstpage’. Obviously you need to turn that into an if/else or something.

No, this won’t help. Basically, what @nigel wants is a pagination object with a different number of elements in each page, so we need to slice up the collection into different pieces and still get a pagination object.

I don’t know if there is an easy way to achieve this without custom methods or a custom pagination class. What is your current code to achieve your workaround?

In my otherwise pretty standard news controller, I have:

$articles = $page->children()->visible();

// append a junk node
$articles = $articles->append("trash","trash")->flip();

// paginate
$articles = $articles->paginate(($perpage >= 1)? $perpage : 5);

// remove the junk node: page 1 now has one less item
if($articles->pagination()->isFirstPage()): $articles = $articles->offset(1); endif;

Works like a charm, but it feels sloppy—and I’m not sure if I’m using append quite right.

2 Likes

I would have suggested the same, can’t really think of an easier way to achieve this. But who knows, maybe someone else has a better idea.

Well, in any case, that eases my mind! Thank you.