Excerpt and Images


I’m currently building a blog template and stumbled over the issue that if I use excerpt() on the overview and the article starts with an image, the image gets strip out. Any idea how to avoid that without adding something like a featured image or do I have the wrong expectation or miss something?


And with Image I mean basically everything else than plain text. :smile:

I’d use a separate field to select a cover image for each post.

<article class="post">
    <img class="cover" src="<?= $page->cover() ?>" alt="" />
    <p class="excerpt"><?= $page->text()->excerpt() ?></p>

The excerpt function in Kirby core says “Creates an excerpt without html and kirbytext” so yeah, it strips html elements.

The excerpt function from the toolkit has an option for this though, as the third argument to the function, so you could create your own function or plugin (or perhaps override it? I’m not familiar enough yet with Kirby to be sure) and do something like:

function excerpt($text, $length = 140) {
  return str::excerpt(kirbytext($text), $length, FALSE);

I tried to modify helpers.php and it works, but of course it’s a change to Kirby core and should be done in a cleaner way.

If you follow Malvese’s approach to solve your problem, make use of custom field methods so you don’t need to make changes to the core.

taking into account that I’m relatively new to kirby and my development skills are a bit rusty I think I’ll go with the Cover/Featured Image after all.


@twirx I now added a Cover Image Field to the Article Blueprint, as a textfield. Is it possible to drag n’ drop images from the files into something else than a textarea, I’m stuck neither type: text nor url gives me that functionality.

Kirby offers a variety of different field types that allow you to fully customize your backend. You might have a look at the documentation which has a very useful overview of all available field types.

I’d use the select field type with dynamically populated select options or the new visual file select field type (Github) by @DieserJonas.

Nice, I’ll try to use the visual file select - thanks.

Just chiming in, as you don’t need to modify any core stuff – it’s sufficient to call the toolkit function in the template as you proposed:

echo str::excerpt($page->text()->kirbytext(), 200, FALSE);

This runs the HTML result of the kirbytext through the excerpt function, returning HTML if set to false. (see docs).

Should be noted that the length count respects characters only and that it’s complicated to control the amount of HTML objects, eg. limit to only one image or element. One is better off with an optional teaser field in these cases.