# Display arbitrary number of blog posts

**URL:** <https://forum.getkirby.com/t/display-arbitrary-number-of-blog-posts/8211>\
**Category:** Questions\
**Tags:** v2\
**Created:** [August 9, 2017, 1:44pm UTC](https://forum.getkirby.com/t/display-arbitrary-number-of-blog-posts/8211 "2017-08-09T13:44:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![flobin](https://avatars.discourse-cdn.com/v4/letter/f/dec6dc/32.png) [@flobin](https://forum.getkirby.com/u/flobin)\
**Post date:** [August 9, 2017, 1:44pm UTC](https://forum.getkirby.com/t/display-arbitrary-number-of-blog-posts/8211/1 "2017-08-09T13:44:54Z")

</div>

I am trying to include some (I don’t yet know how many) blog posts on the home page. I would like to have a few posts, then some other information in between, and then some more posts. What I’ve tried in my `home.php` template is this:

```auto
<?php $articles = page('nieuws')->children()->visible()->flip();
    $first_set = array_slice($articles, 0, 2);
    $second_set = array_slice($articles, 2, 4);

    foreach($first_set as $article): ?>

        <article>
            <a href="<?php echo $article->url() ?>">
                <?php $image = $article->coverimage()->toFile();
                if($image): ?>
                <img src="<?= $image->url() ?>" alt="">
                <?php endif ?>
                <h2 class=""><?php echo $article->title()->html() ?></h2>
            </a>
        </article>

    <?php endforeach ?>

```

However I get this error:

```
array_slice() expects parameter 1 to be array, object given

```

So obviously I can’t array\_slice() an object. Is there a way to easily return an arbitrary number of blog posts?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 9, 2017, 1:48pm UTC](https://forum.getkirby.com/t/display-arbitrary-number-of-blog-posts/8211/2 "2017-08-09T13:48:24Z")

</div>

`$articles` is not an array, but an object of type `Pages`, so you can’t use array methods.

Kirby has the [`offset()`](https://getkirby.com/docs/cheatsheet/pages/offset) and [`limit()`](https://getkirby.com/docs/cheatsheet/pages/limit) methods to achieve what you want.

```auto
$first_set = $articles->limit(2);
$second_set = $articles->offset(2)->limit(4);

```

---

<div class="post-metadata">

**Author:** ![flobin](https://avatars.discourse-cdn.com/v4/letter/f/dec6dc/32.png) [@flobin](https://forum.getkirby.com/u/flobin)\
**Post date:** [August 9, 2017, 1:50pm UTC](https://forum.getkirby.com/t/display-arbitrary-number-of-blog-posts/8211/3 "2017-08-09T13:50:07Z")

</div>

Thank you, that’s exactly what I needed! (In your example it should also be `->limit(2)` in the second line.)

---

<div class="post-metadata">

**Author:** ![flobin](https://avatars.discourse-cdn.com/v4/letter/f/dec6dc/32.png) [@flobin](https://forum.getkirby.com/u/flobin)\
**Post date:** [August 9, 2017, 2:33pm UTC](https://forum.getkirby.com/t/display-arbitrary-number-of-blog-posts/8211/4 "2017-08-09T14:33:27Z")

</div>

I think the code here could get repetitive, always including the whole bit in between `<article>` and `</article>`. So I’ve been trying to put this in a snippet.

`homepage_article.php`:

```
<article>
    <a href="<?php echo $article->url() ?>">
        <span class="">tag</span>
        <?php $image = $article->coverimage()->toFile();
        if($image): ?>
        <img src="<?= $image->url() ?>" alt="">
        <?php endif ?>
        <h2 class=""><?php echo $article->title()->html() ?></h2>
    </a>
</article>

```

And `home.php`:

```
<?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);

foreach($first_set as $article):

    snippet('homepage_article');

endforeach ?>

```

However, that doesn’t work. The error I get is: `Undefined variable: article`

Is there a way to set this up as a snippet? I’ don’t know how I could define `$article` in the snippet without having to duplicate the code that selects the set of articles.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 9, 2017, 2:37pm UTC](https://forum.getkirby.com/t/display-arbitrary-number-of-blog-posts/8211/5 "2017-08-09T14:37:43Z")

</div>

You can pass variables to snippets: [https://getkirby.com/docs/templates/snippets/#passing-variables-to-snippets](https://getkirby.com/docs/templates/snippets/#passing-variables-to-snippets)

---

<div class="post-metadata">

**Author:** ![flobin](https://avatars.discourse-cdn.com/v4/letter/f/dec6dc/32.png) [@flobin](https://forum.getkirby.com/u/flobin)\
**Post date:** [August 9, 2017, 2:38pm UTC](https://forum.getkirby.com/t/display-arbitrary-number-of-blog-posts/8211/6 "2017-08-09T14:38:29Z")

</div>

> [@texnixe](#):
>
> [https://getkirby.com/docs/templates/snippets/#passing-variables-to-snippets](https://getkirby.com/docs/templates/snippets/#passing-variables-to-snippets)

Thanks so much, I probably should have found that myself.

---

<div class="post-metadata">

**Author:** ![David](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@David](https://forum.getkirby.com/u/David)\
**Post date:** [December 6, 2023, 1:19am UTC](https://forum.getkirby.com/t/display-arbitrary-number-of-blog-posts/8211/8 "2023-12-06T01:19:44Z")

</div>

`<?php foreach($page->children()->listed()->flip()->limit(2) as $article): ?>`

should work

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:22pm UTC](https://forum.getkirby.com/t/display-arbitrary-number-of-blog-posts/8211/9 "2025-02-06T17:22:52Z")

</div>


