A small plugin for Kirby 2 that creates excerpts from fields but keeps the html tags intact.
Sometimes it can be useful to be able to trim a long article down so it can be displayed in something like a blog list. In an ideal world, you would write this text by hand but it can be useful to have a fallback solution if you are pushed for time.
Unlike Kirby's built in Excerpt field method which only returns plain text, this plugin creates excerpts from fields via kirbytext but keeps HTML tags. This means all tags, including images. You can set a character or word limit.
To use this plugin, place all the files in
Defaults to words and an ellipsis, so if you just want 20 words and an ellipsis on the end:
<?= $page->text()->chopper(20) ?>
To trim to 200 characters and append ellipsis on the end:
<?= $page->text()->chopper(200, 'chars') ?>
To trim to 50 words and append an arrow on the on the end:
<?= $page->text()->chopper(500, 'words', '→') ?>
To change the default list of kept tags, add this line to your
config.php and amend accordingly:
This plugin was built using Kirby 2.5+. May work on earlier versions.
Based on the work of Partrick Galbraith (https://www.pjgalbraith.com/truncating-text-html-with-php/)
We would love to work with you. Get in touch today to see how we can help you grow your company online.Contact Us