Count articles and display article number

I’m new here, so please be kind!

I would like to count the number of articles in the Opinion (Blog) section of my site, then return the article number to add as a pre-fix to its title. Example design below:

This how I am displaying the article list at the moment:

<span><b><?= $article->date('F jS, Y') ?></b></span>
<p class="lead unmarg"><a href="<?= $article->url() ?>"><?= $article->title()->html() ?></a> 
</p>
<p><?= $article->text()->kirbytext()->excerpt(50, 'words') ?></p>

Any help massively appreciated. Thanks.

When you loop through the list of articles, you can use the indexOf() method to get the index of the article (in relation to the complete article collection). Since it starts at 0, we add 1:

<?php foreach ($articles as $article) : ?>
<?= '#'.$articles->indexOf($article) + 1 . ' - ' . $article->title() ?>
<?php endforeach ?>

Yes makes complete sense, thank you - but it is throwing an error :frowning:
What can I post to help debug?

(really appreciate your amazing support!)

My bad, corrected above, should be $articles as $article

If there’s still an error, the error message itself would be useful.

Hmm, still showing an error (standard kirby “unexpected error”). Code pasted below if useful?

<?php if($articles->count()): ?>
	 	<?php foreach($articles as $article) : ?>
            <section class="space--xs blog-article-wide">
                <div class="container">
                    <div class="row">
                        <div class="text-center-xs">
                            <div class="col-sm-3">
                                <div class="">
                                    <a href="<?= $article->url() ?>">
                                        <?php snippet('coverimage', $article) ?>
                                    </a>
                                </div>
                            </div>
                            <div class="col-sm-7">
								<span><b><?= $article->date('F jS, Y') ?></b></span>
                                <p class="lead unmarg"><a href="<?= $article->url() ?>">
									<?= '#'.$articles->indexOf($article) + 1 . ' - ' $article->title() ?></a></p>
								<p><?= $article->text()->kirbytext()->excerpt(50, 'words') ?></p>
                            </div>
                            <div class="col-sm-2 text-right text-center-xs">
								<span class="type--fine-print">by Andrew Craig</span>
                                <p class="type--fine-print"><em>Founder &amp; CEO</em></p>
								<a class="btn type--uppercase" href="<?= $article->url() ?>">
                                    <span class="btn__text">
                                        Continue Reading
                                    </span>
                                </a>
                            </div>
                        </div>
                    </div>
                    <!--end of row-->
                </div>
                <!--end of container-->
            </section>
	      <?php endforeach ?>

Could you please enable debugging in your config.php`

c::set('debug', true);

to get a useful error message.

Ok, I was being blind, there was a dot missing:

<?= '#' . $articles->indexOf($article) + 1 . ' - ' . $article->title() ?></a></p>

A different error now. (So sorry this is such a pain for you!!)

 <?= '#' . ($articles->indexOf($article) + 1)  . ' - ' . $article->title() ?>

Excellent, now working, thank you. :partying_face::partying_face:

Now two more issues:

  1. Because I use pagination, the articles are number 1-10 on each page rather than their relative number in the overall sequence i.e. on page 2, the articles should be numbers 11-20 not 1-10 again.

  2. The numbers are in the wrong order (reverse) ie. I would like the newest article to be the largest number not number 1.

Thanks again

For the second issue, we can just turn the numbering around:

($articles->count() - $articles->indexOf($article))

Thanks yes, that solves point 2:

<?= '#' . ($articles->count() - $articles->indexOf($article) + 1)  . ' - ' . $article->title() ?>

Any thoughts on the pagination issue?

(#keepingyoubusytoday)

No, not out of my head. If I’m not overthinking it, it probably needs some calculation taking into account the pagination limit and the pagination page.

Thanks for your help today. :+1: