Using pagination on offset blog articles

I have a front page that shows blog posts and the blog posts are broken up in sections, like so:

<?php $articles = page('nieuws')->children()->visible()->flip();
$first_set  = $articles->limit(2);
$second_set = $articles->offset(2)->limit(2);
$third_set  = $articles->offset(4)->limit(2);
$fourth_set = $articles->offset(6)->limit(2);
$fifth_set  = $articles->offset(8)->limit(2);
$rest       = $articles->offset(10)->paginate(2);

The snippets for each page are like this (the tags part is unrelated):

<?php foreach($second_set as $article):

    $tags = $article->tags()->split(',');
    snippet('homepage_article', array('article' => $article, 'tags' => $tags));

endforeach ?>

After the last set of articles, I’d like to paginate, so I’m including the pagination snippet (same as in the starter kit):

<?php snippet('pagination') ?>

However, I get this error:

Undefined variable: pagination

It’s in the first line of the pagination snippet:

<?php if($pagination->hasPages()): ?>

I have 15 posts.

Is it at all possible to paginate articles in this way?

You have to define $pagination:

// …
$rest       = $articles->offset(10)->paginate(2);
$pagination = $rest->pagination();

Thank you for the quick response! I have set pagination on $rest, but I’m still getting the same error. Should the pagination snipped be called in a different way? Or the articles?

This is all of my home.php by the way and this is the stack trace.

Ah, ok, it’s defined in the template. You need to pass the variable to the snippet, then.

<?php snippet('pagination', array('pagination' => $pagination)) ?>
1 Like

Thanks so much!

So now of course only the last set of articles is being paginated and the first couple of articles are still there. That’s not great.

I guess my real question is this: is it possible to ‘inject’ non-blog-posts in the blog post loop? (Really only on the first page in this case.)

Well, you need an if statement:

if($pagination->isFirstPage()) {
  // show the non-paginated stuff only on the first page
}
1 Like

Thanks so much!

Small correction, it should be:

if($pagination->isFirstPage()) {
  // stuff
}

Oh, yes, of course. Will correct it above.