Limiting number of Tags

Hey people :wink:

I try to figure out how to limit the output of tags…

controller:

<?php

return function ($page) {
    return [
        'tags' => page('any-page')->tags()->split(','),
    ];
};

template:

<?php foreach ($tags->limit(x) as $tag): ?>
      …anything…
<?php endforeach ?>

This doesn’t work :wink:
Hope you can help me!

THX once again.

$tags is a simple array, so use standard PHP array_slice() PHP: array_slice - Manual

Hey,

thanks for your answer but I can’t handle this ;(
Iam not a pro in PHP. So please can you explain it a little bit more detailed?

  $tags = array_slice($tags, 0, 5);

Alternative: Create a collection from the tags

$tags = new \Kirby\Toolkit\Collection($tags);

 foreach($tags->limit(5) as $tag) {
   echo $tag; 
 }

Amazing!
Big THX.