Show the three newest Projects on Homepage

Currently, I am learning and fighting to get along in Kirby 3. I’ve started with the starter kit. So it is a beginner question. I’ve copied the notes content to build a project content. This is working quite well. I now want to show the newest three Projects on the start site, with image, excerpt, Title and link. I’ve tried to solve this with a snippet. But I am lack knowledge of how to write the foreach loop. It is unclear what to write there and how to access the sites. I’ve not found a tutorial for that.

I’ve tried several things:

<?php foreach (page('projects')->children()->visible()->limit(3) as $project): ?>
<?php foreach ($projectPage->children()->visible()->limit(3) as $project): ?>
<?php foreach($data->children()->listed() as $project): ?>

So first we need to be sure we’re on the same page regarding what file you’re editing.
If you say, you want to show them on the start site, I’d think you’re adding that foreach loop to site/templates/home.php, correct?

Then page('projects') looks like a good start. Cause the home page doesn’t first know from what parent page you want to show these. If that’s projects in your case, that’s right.

->children() is fine, ->visible() is an old Kirby 2 term (which is still legacy supported) - the newer equivalent would be ->listed().

->limit(3) is also fine to just get 3.

What’s missing is the “newest” requirement. How would “newest” be defined in your case? Is there a date field for each project? Or just the latest numbered one (so newest = last listed three)?

At site/templates/home.php I have a <?php snippet('projects3') ?>

And at site\snippets\projects3.php I wanted to build the foreach loop.

How would “newest” be defined in your case? Is there a date field for each project? Or just the latest numbered one (so newest = last listed three)?

The last three created/listed projects.

The simplest would be something as

<?php foreach (page('projects')->children()->listed()->flip()->limit(3) as $project): ?>
  <a href="?= $project->url() ?>"><?= $project->title() ?></a>
<?php endforeach ?>

which implies that you add each new project at the end of the list. We just ->flip() the list and take only 3.

1 Like

That worked, thank-you.

Now I am heading into the upcoming problems. Should I open a new forum post or just append here?

My Snippet now looks like that:

<h2>Neueste Projekte</h2>
  <ul class="home-grid">
<?php foreach (page('projects')->children()->listed()->flip()->limit(3) as $project): ?>
  	<li>
    		<h3><a href="<?= $project->url() ?>"><?= $project->title() ?></a></h3>
    		<p><?= $project->text()->excerpt(80) ?> <a href="<?= $project->url() ?>">mehr&nbsp;dazu</a></p>
    			<?php if($image = $project->images()->sortBy('sort', 'asc')->first()): ?>
    				<a href="<?= $project->url() ?>">
      				<img src="<?= $image->url() ?>" alt="<<?= $project->title()->html() ?>" >
    				</a>
    			<?php endif ?>
  	</li>
   <?php endforeach ?>
   </ul>

And what’s the problem with that?

The result looks a bit weird and there is no excerpt or headings at all:

I have now been inspired by the code from template/project.php:

<h2>Neueste Projekte</h2>
  <ul>
<?php foreach (page('projects')->children()->listed()->flip()->limit(3) as $project): ?>
  	<li>
  	
<article class="project-excerpt">
  <a href="<?= $project->url() ?>">
    <header>
      <figure class="img" style="--w: 16; --h:9">
        <?php if ($cover = $project->cover()): ?>
        <?= $cover->crop(320, 180) ?>
        <?php endif ?>
      </figure>

      <h3 class="project-excerpt-title"><?= $project->title()->esc() ?></h3>
    </header>
    <?php if (($excerpt ?? true) !== false): ?>
    <div class="project-excerpt-text">
      <?= $project->text()->toBlocks()->excerpt(280) ?>
    </div>
    <?php endif ?>
  </a>
</article>
  	</li>
   <?php endforeach ?>
  	<li><a href="/projects">weiteres Engagement</a></li>
   </ul>

Now it looks like I want it to:

<ul class="grid">
    <?php foreach (page('projects')->children()->listed()->flip()->limit(3) as $project): ?>
    <li class="column" style="--columns: 4">
        <?php snippet('project', ['project' => $project]) ?>
    </li>
    <?php endforeach ?>
   </ul>
<p><a href="/projects">weiteres Engagement</a></p>