I am implementing a search and would like to include an excerpt of the text surrounding the query. As far as I have gathered by searching the forum, I would have to write my own function/plugin for this, but as I’m not very proficient at PHP, I am having some trouble.
I found this snippet by @texnixe which looks like what I would need. However, I am searching all fields and not just text fields, which is probably why this is not working for me.
… and in case there is no solution to this when filtering through all field types, I would still be very grateful for a more detailed explanation of how exactly to implement @textnixe’s method that I mentioned. I tried registering it as a Pages method but to no avail. I am copying it here for clarity:
Alright, I created the folder site/plugins/searchexcerpt and copied the code into an index.php. Sorry for being stupid, but then how exactly would I use it…? I tried this, which doesn’t return anything for $text:
Awesome, thanks so much! I had to modify your solution a little to fit with my field names, so for anybody else who isn’t getting a result with the above answer, try this:
<?= getResultText($result->description(), $query) ?> <?= getResultText($result->someotherfield(), $query) ?>
etc.