Excerpt method doesn't strip caption from image kirbytag

I’m using the excerpt method on some article text that often begins with an image kirbytag. (I prefer to use the kirbytag here instead of creating a cover image field.)

The excerpt correctly strips the “image:…” and “class: …” definitions in the kirbytag, but it seems to ignore the “caption: …” definition. So my exerpts often begin with an image caption!

I’m new to Kirby (just bought my first licence) and just beginning with php, so thanks for any help.

The excerpt() method shortens the text in your field. It doesn’t care where this text comes from, so this is not surprising.

To avoid that, there are several strategies:

  1. Create a separate field for a short intro text. That way, you have full control over your text and don’t have to care what your full text starts with.
  2. Use a cover image instead of starting your articles with an image. Make sure that the articles always start with a useful paragraph.
  3. Create your own excerpt method that ignores kirbytags.

Hi Sonja,

Thanks for your swift reply, and your suggestions. I can see why the excerpt() method retains all the caption text it finds.

  1. I’ve already got an intro field, but I want to also display a separate excerpt on a listing page.

  2. I’ve tried a cover image, and but I’d like to keep all my image captions in a single markdown field along with the text. I really like how this is done in the Kirby panel — it’s compact, easy to edit, and in a highly portable format.

  3. So that leaves creating my own excerpt method…

I’ve not got the skills to do that at the moment, but if it’s just a few lines of code, I’d appreciate any help and guidance on how to get started.

Yes, I understand that. I’d probably try and find the first p tag and get the text from that element. But it’s not so easy, if that paragraph is very short, then the excerpt would also be short. Or if you use custom Kirbytags that contain text you actually want to use. Therefore, in my opinion, the separate intro field is the best choice and I’d always prefer that to any auto-excerpt method.

Sonja:

The idea of searching for the first p tag sounds good. I searched in the Forum and found this:

https://forum.getkirby.com/t/variable-length-excerpts-possible/1073/4

In this thread, you have suggested some code for a getFirstPara() function. I tried to get this to work yesterday, creating a new php file in Controllers, but something went wrong. I’ll try again next week. I need to learn a bit more php too!

There’s another suggestion from you in the same thread, and this extracts everything from the first <p> tag until a comment tag <!-- more -->. This might be my best solution.

Thanks again for your suggestions!

The code suggested in that old thread should actually still work.

The best approach is to put this method into a plugin. Create a new folder inside the plugin folder (e.g. /methods), inside that folder an index.php and then inside that file, put the function. Make sure to start the file with a <?php tag.

Perfect! Worked first time. Thanks.

1 Like

Unfortunately, my last post was premature!

Both these plugins exhibit weird behaviour with my templates:

The getFirstPara method works perfectly on articles with a paragraph as the first line in the KirbyText. But on articles with an image KirbyTag as the first line, the method extracts two extra lines of text after the end of the first paragraph!

The getExcerpt method words correctly on all articles , except that it does something to the text somewhere (I guess it’s removing a closing <div> or <p> ) that messes up the column layout!

Sorry I didn’t test these properly before my last post. I will try to investigate further next week.

Have a nice weekend. If you want, provide your template code and an example text file where it fails.

I’ve adapted texnixe’s earlier suggestion at https://forum.getkirby.com/t/variable-length-excerpts-possible/1073/4 to the following:

<?php 
function getExcerpt($string){
    $start = strpos($string, '<p>');
    $string = substr($string, $start, 350);
    $string = strip_tags($string, '<strong>');
    return $string . "…";
    }
?> 

I added this as a new index.php file in my new plugin folder, in a folder called Methods.

This works exactly as I wanted in my original post. The getExcerpt method ignores any KirbyTags at the beginning of the KirbyText file, starting at the first <p> , counting up the to the number of characters set in the attributes. I added strp_tags to remove any unwanted paragraph breaks.

Thanks Sonja for your help!

2 Likes

Here are a couple more solutions to my original problem of creating an excerpt from a KirbyText field, without including (or getting confused by) any KirbyTags at the top of the field:

  1. An excerpt of the first (300) characters

    <?php 
     function excerptToChars($string){
     $start = strpos($string, '<p>');
     $string = substr($string, $start, 300);
     $string = strip_tags($string, '<strong>');
     return $string . "…";
     }
     ?>
    
  2. An excerpt up to a markdown comment <!-- more -->

    <?php 
     function excerptToMore($string){
       $start = strpos($string, '<p>');
       $string = substr($string, $start);
       $length = strpos($string, "<!-- more -->" );
       $string = substr($string, 0, $length);
       $string = strip_tags($string, '<p> <ul> <ol> <li> <strong>');
       return $string;
     }
    ?>
    
  3. An excerpt comprising the complete article text, minus figures, images etc. This is useful for including short, simple posts on a blog index page.

    <?php 
     function articleTextOnly($string){
     $start = strpos($string, '<p>');
     $string = substr($string, $start);
     $string = strip_tags($string, '<p> <ul> <ol> <li> <strong>');
     return $string;
     }
    ?>