<p>Looking at the Happy Lager demo site there's a Short Description field, which functions as an excerpt for indexes and search results. I imagine P&amp;T use something similar for the Craft docs.</p>

<p>You could also force your results onto one line and trim it with css, which is how P&amp;T do it on the Craft docs search results page:</p>

<pre><code>li { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
</code></pre>

CC BY-SA 3.0
2015-08-20T11:56:39.057Z
2015-08-20T11:56:39.057Z
Paul Grant
<p>Have a look at the <a href="http://twig.sensiolabs.org/doc/filters/slice.html" rel="nofollow noreferrer"><code>slice</code></a> filter. To return 140 characters max. you would do something like this:</p>

<pre><code>{% if entry.body %}
    {{ entry.body|slice(0, 140) }}
{% endif %}
</code></pre>

<p>See <a href="https://craftcms.stackexchange.com/q/4361/125">this other question</a> on a similar topic if you want to apply this on a rich text field. Because this solution only works with plain text.</p>

CC BY-SA 4.0
2018-09-19T15:58:31.630Z
This thew an exception for me. `Calling unknown method: craft\elements\Entry::body()` I am using craft 3
Radmation
CC BY-SA 4.0
2018-09-19T16:05:30.403Z
Is that your field handle, `body`?
carlcs
CC BY-SA 3.0
2014-11-18T22:07:22.960Z
2014-11-18T22:16:02.637Z
2017-04-13T12:51:00.303Z
carlcs
<p>I use <a href="https://github.com/rkingon/Craft-Plugin--Trimmer" rel="nofollow">Trimmer</a> for this, it's pretty much the only plugin I use these days.</p>

<p>I don't know for sure, but I suspect P&amp;T may use an 'excerpt' field to store a short sentence for use on the search results page, rather than cropping from longer copy.</p>

CC BY-SA 3.0
2014-11-19T09:25:19.217Z
Do I understand it right and this does remove all HTML tags? Again a deal breaker for rich text fields but otherwise a great little plugin. I especially like the option to trim before full words only.
carlcs
CC BY-SA 3.0
2014-11-19T09:33:48.463Z
Yes, it does strip HTML. There was a plugin for EE that would allow HTML and close any tags left open after trimming, I must try and find it to see if it can be/has been ported.
Steve Abraham
CC BY-SA 3.0
2017-02-13T18:19:30.017Z
I added pull request for option to keep html tags [github](https://github.com/rkingon/Craft-Plugin--Trimmer/pull/7)
Vadim Goncharov
CC BY-SA 3.0
2014-11-19T09:19:30.850Z
2014-11-19T09:19:30.850Z
Steve Abraham
<p>If I enter a query into the search bar I would like to see an excerpt (let's say 140 characters) of each entry that is related to the query. </p>

<p>Here is what I use on the search result page to display the results.</p>

<pre><code>{% set query = craft.request.getParam('q') %}
{% set entries = craft.entries.search(query).order('score') %}

{% if entries|length %}
&lt;p&gt;{{ entries|length }} results:&lt;/p&gt;

&lt;ul&gt;
    {% for entry in entries %}
        &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
{% else %}
&lt;p&gt;Your search for “{{ query }}” didn’t return any results.&lt;/p&gt;
{% endif %}
</code></pre>

<p>How do I add entry excerpts like: <a href="https://craftcms.com/search?q=entry" rel="nofollow noreferrer">https://craftcms.com/search?q=entry</a></p>

<entries><search>
Get excerpt of entry from search query
# Get excerpt of entry from search query

(Asked by: username_0 on 2014-11-18)

If I enter a query into the search bar I would like to see an excerpt (let's say 140 characters) of each entry that is related to the query. 
Here is what I use on the search result page to display the results.
{% set query = craft.request.getParam('q') %}
{% set entries = craft.entries.search(query).order('score') %}

{% if entries|length %}
{{ entries|length }} results:


    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}

{% else %}
Your search for “{{ query }}” didn’t return any results.
{% endif %}

How do I add entry excerpts like: https://craftcms.com/search?q=entry

**Tags:** entries, search


---

## 3 answers

### Answer by username_1 on 2015-08-20. Score: 3

Looking at the Happy Lager demo site there's a Short Description field, which functions as an excerpt for indexes and search results. I imagine P&T use something similar for the Craft docs.
You could also force your results onto one line and trim it with css, which is how P&T do it on the Craft docs search results page:
li { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }


---

### Answer by username_2 on 2014-11-18. Score: 3

Have a look at the slice filter. To return 140 characters max. you would do something like this:
{% if entry.body %}
    {{ entry.body|slice(0, 140) }}
{% endif %}

See this other question on a similar topic if you want to apply this on a rich text field. Because this solution only works with plain text.

> **Comments:**
> **username_3**: This thew an exception for me. `Calling unknown method: craft\elements\Entry::body()` I am using craft 3  
> **username_2**: Is that your field handle, `body`?  

---

### Answer by username_4 on 2014-11-19. Score: 2

I use [Trimmer](https://github.com/rkingon/Craft-Plugin--Trimmer) for this, it's pretty much the only plugin I use these days.

I don't know for sure, but I suspect P&T may use an 'excerpt' field to store a short sentence for use on the search results page, rather than cropping from longer copy.

> **Comments:**
> **username_2**: Do I understand it right and this does remove all HTML tags? Again a deal breaker for rich text fields but otherwise a great little plugin. I especially like the option to trim before full words only.  
> **username_4**: Yes, it does strip HTML. There was a plugin for EE that would allow HTML and close any tags left open after trimming, I must try and find it to see if it can be/has been ported.  
> **username_5**: I added pull request for option to keep html tags [github](https://github.com/rkingon/Craft-Plugin--Trimmer/pull/7)  

---

