Count articles published in a year

Good Morning together,

I use this code snippet for my archive box. It displays all the years where articles have been published. Now I want to count how many articles are in each year but I cant really figure it out though…

   <ul>
      <?php function pageYear($p) { return $p->date('Y'); } ?>
      <?php $y = param('year') ?>
      <?php foreach ($all_articles->group('pageYear') as $year => $yearList): ?>
        <li>
          <a class="<?php ecco($year == $y, 'active') ?>" href="<?= url($page->url() . '/' . url::paramsToString(['year' => $year])) ?>"><?= $year ?></a>
         <!-- This span should show how many articles are posted in this year --> 
          <span><?= $all_articles->filter(function($p) use($year) {return $p->date('Y') === $year;})->count() ?></span>
        </li>
      <?php endforeach ?>
    </ul>

Thanks for any advice :slight_smile:

$yearList->count() should give you the number of articles for each year.

1 Like

Thanks @texnixe :slight_smile: works like a charm