# Getting two items at a time from a loop

**URL:** <https://forum.getkirby.com/t/getting-two-items-at-a-time-from-a-loop/21299>\
**Category:** Questions\
**Tags:** v3\
**Created:** [February 21, 2021, 12:45pm UTC](https://forum.getkirby.com/t/getting-two-items-at-a-time-from-a-loop/21299 "2021-02-21T12:45:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SCC](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/scc/32/4116_2.png) [@SCC](https://forum.getkirby.com/u/SCC)\
**Post date:** [February 21, 2021, 12:45pm UTC](https://forum.getkirby.com/t/getting-two-items-at-a-time-from-a-loop/21299/1 "2021-02-21T12:45:50Z")

</div>

Hi,

I am building a timeline, having pulled all the timeline items through the controller, I can create a loop easily enough to get all of the items. But what I want to do (example below) is get all timeline items then loop through two of them at a time. The reason is that I want to display them a specific way.

I am not sure how to do this, clearly I need to get all the items which I can do, but then I need to then process two items at a time until the loop is out of items. Does that make sense? Can anyone suggest a way to do this?

```
<?php foreach($items as $item): ?>
    <?= Display 1st item this way ?>
    <?= Display 2nd item a different way ?>
<?php endforeach ?>

```

Thanks for your valuable assistance as always.

Lee

---

<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:** [February 21, 2021, 12:49pm UTC](https://forum.getkirby.com/t/getting-two-items-at-a-time-from-a-loop/21299/2 "2021-02-21T12:49:36Z")

</div>

The best way is probably to create chunks of two:

> **[$pages-\>chunk()](https://getkirby.com/docs/reference/objects/pages/chunk)**
>
> Creates chunks of the same size.

```auto
<?php 
$chunks = $items->chunk(2);
foreach ( $chunks as $chunk ) : 
?>
<div class="chunk-wrapper">

  <?php foreach ( $chunk as $item) : ?>
    <?php if ( $item->indexOf($items) === 0 ) : ?>
        <div class="first-item">This is the first item</div>
    <?php else: ?>
        <div class="second-item">This is the second item</div>
    <?php endif ;?>
</div>
<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![bruno](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bruno/32/6422_2.png) [@bruno](https://forum.getkirby.com/u/bruno)\
**Post date:** [February 21, 2021, 2:37pm UTC](https://forum.getkirby.com/t/getting-two-items-at-a-time-from-a-loop/21299/3 "2021-02-21T14:37:02Z")

</div>

Another way:

```auto
<?php $i=0; foreach($items as $item): $i++; ?>
    <?php if ($i%2): ?>
        <?= Display 1st item this way ?>
    <?php else: ?>
        <?= Display 2nd item a different way ?>
    <?php endif; ?>
<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![SCC](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/scc/32/4116_2.png) [@SCC](https://forum.getkirby.com/u/SCC)\
**Post date:** [February 21, 2021, 3:28pm UTC](https://forum.getkirby.com/t/getting-two-items-at-a-time-from-a-loop/21299/4 "2021-02-21T15:28:51Z")

</div>

Thanks Bruno, that makes complete sense and works perfectly, thank you very much.
