Excerpt with formatted kirbytext

Hi,

I am using this right now
<?php echo $article->text()->excerpt(300) ?>

but it gives me just unformatted text.

But I would like to preview whatever is in the first part of the blog post untill to a certain point. So, if there is like 100 characters of text and a video in the first part of the blog post, I would like it to show up on the blog post list page. Same with images or whatever.

Also it would be nice to have the text formatted, like with an h3 or whatever, just like in the blog post.

How can I do that?

I also tried this:
<?php echo str::excerpt($page->text()->kirbytext(), 120, false) ?>

But it just didn’t show anything.

The safest way to do that is an extra field that contains everything you want in the excerpt. If you use

<?php echo str::excerpt($page->text()->kirbytext(), 120, false) ?>

you never know what you get, because you might end up with unclosed tags which can yield unusable results.

Oh - ok.

So - I made a new text area field with kirbytext and I manually cut off the excerpt, where I want to. No Cover Image anymore - I do it manually. I do all the excerpt styling and content manually. The good thing - it does the job very well.

Its basically a copy of the Kirbytext text area just that its called β€œexcerpt”.

Since this is so much better than the excerpt-excerpt, I wonder - what is the use of the excerpt function? Because its a robot doing it?

Anyway - thank you!

Well, the standard excerpt field method $page->text()->excerpt() is not meant to have any styling.

I think in another post I suggested using <?php echo str::excerpt($page->text()->kirbytext(), 120, false) ?> but yesterday I realized that this gives me funny results because of unclosed tags.

What I used to do myself is to get the first paragraph of a text

   function getFirstPara($string){
        $string = substr($string,0, strpos($string, "</p>")+4);
        return $string;
    } 

This will return everything from the beginning of the text until the first closing p tag. So if you have an image at the very beginning, followed by a paragraph, it will return that. Did a good job for my purposes, but I still think that the extra excerpt field is a better choice because it gives you 100% control at the price of a little more work. The up side is that you get a chance to spice up your intro text.

Happy writing :slight_smile:
S.

1 Like