Hi there – I’m taking Kirby for a spin as a possible WP replacement. I will use Kirby as a blog, and I’m following the very basic instructions in the documentation to set up a simple blog environment. One feature I like in WP is being able to manually insert a “continue reading” break into in each article at the place of my choosing, instead of calling a standardized excerpt() function after so many words for each entry on the main blog-overview page. Is there a way to do this in Kirby?
Thanks for any pointers,
O.H.
Any text field can be trimmed or excerpted to a specified length by calling its excerpt
method:
$page->text_field()->excerpt(500);
At the moment, this defines the number of characters, but (if my memory serves me correctly), there is an alternate word-count truncation method in the works.
Check out the docs for more info!
Edit: Maybe this is what you’ve tried already?
Hi August – Thanks for responding. When calling the function as part of a loop, for example to display an x number of words from each blog entry on the main page, the excerpts will all be the same length, if I understand correctly. What I’m trying to figure out is whether there is a way to define – for each blog entry separately – how much of it will be displayed before the “continue reading” link is generated. Sorry if I sound confusing.
Ah, I think I see what you’re after— a tag you might insert into your markdown to say “stop here!” but at an unknown distance into the text.
You may need to look into cusom Kirbytags to be able to do this elegantly.
I once create a simple getFirstPara function which you could adapt:
<?php
function getFirstPara($string){
$start = strpos($string, '<p>');
$string = substr($string,$start, strpos($string, "</p>")+4);
return $string;
}
?>
In your markdown, you could insert a comment
<!-- more -->
and then get everything before that comment
<?php
function getExcerpt($string){
$start = strpos($string, '<p>');
$string = substr($string,$start, strpos($string, "<!-- more -->"));
return $string;
}
?>
Then in your template
<?php echo getExcerpt($page->text()->kt()) ?>
The downside is, that this function does not output anything if the read more tag is missing but you could extend this further. [Edit] Also, anything before the first opening p-element will be discarded as well.
[Edit:] You may also have a look at this post that uses explode for an alternative method https://www.texniq.de/en/blog/kirby-cms-split-long-posts-into-pages (needs to be adapted for the current use case though)
[Edit] An alternative would be having a separate excerpt field for total control of the text that appears as an excerpt.
Hi texnixe – Your suggestion of a separate excerpt field sounds like it would be the best way to go. Of course, being new to Kirby, I will have to figure out how to do that and then adapt the blog-entries-display loop accordingly. At this point, I’m just trying to ensure that it can be done, conceptually, before making the switch from Wordpress. You can see here, www.thecapitalist.net, how I’ve been using the idea. It’s very easy in WP, simply by insert the kind of comment you mentioned in one of your suggestions. For me, it’s particularly important to be able to control if images appear either above or below the fold.
As said, this is totally doable with Kirby – the question rather is which way you wanna go:
- Split up the text content based on a marker (e.g.
<!-- more -->
) - Have an extra excerpt field which will be used on the overview and then together with the main content field in the single view
- Just have another field to define how many characters/words for the excerpt and then do something like:
$page->content()->excerpt($page->excerpt_lenght()->int())
Thanks for that helpful summary, distantnative. Those options make perfect sense. I will mark this thread as solved for now, but may come back to y’all if I can’t figure out the particulars when it comes to that.