How to limit posts per page

Hi guys,

I have a question regarding the blogItems/news.
I got the default blog site, but I also want to display the news on a section of the starting page. So far I’ve managed to make them appear, but the problem is that they all appear, but I just want to let only the 3 latest to appear on the starting page. How do I do that?

Code so far:

  <?php
    $blog = page('blog')->children()->sortBy('date', 'desc');
    foreach($blog as $blogItem): ?>


    <h1 class="newstitel"><?php echo $blogItem->title()->html() ?></h3> <!--News Titel-->

    <p class="newsdatum"><?php echo $blogItem->date('d.m.Y') ?></p> <!--News Datum-->
    <?php if (!empty($blogItem->file())) { ?>
      <img class="newsbild" src="<?php echo $blogItem->file()->url() ?>" />  <!--News Bild-->
    <?php } ?>

    <p class="newstext"> <!--News Text/Inhalt-->
      <?= $blogItem->text()->kirbytext()->excerpt(100, 'words') ?>
      <a href="<?= $blogItem->url() ?>" class="article-more">zum gesamten Artikel</a>
    </p>

  <?php endforeach ?>

You can use the limit() method:

$blog = page('blog')->children()->sortBy('date', 'desc')->limit(3);
1 Like

Thanks for the fast answer! :grinning: