If you’re using Toolbox to output your search results page, you’ve probably noticed you can’t use {{item.excerpt}}
in the {% for item in posts %}
loop. Instead, you will need to use {{item.preview(50)}}
or {{item.content|truncate(50)}}
to get something similar to the excerpt.
As a workaround we’ve added a variable called {{excerpt}}
, which holds the filtered WP excerpt output for the paged items. Please also note that you need to set the Timber Query Method to ‘get_posts’.
What does that mean?
To retrieve an excerpt, use this templatepiece with the {% for item in posts %} {% endfor %}
loop in your Twig-template.
{{ excerpt[ item.ID ] }}
Note: only the excerpts in the posts variable will be callable. If the post is not within the paged set it can not be displayed.
Using the excerpt variable
Here’s an example using the excerpt variable:
{% for item in posts %}
<a href="{{item.link}}"><h3>{{item.title}}</h3></a>
<p>{{excerpt[item.ID]}}</p>
<p><a href="{{item.link}}">{{__('Read More')}}</a></p>
{% endfor %}
Using the Relevanssi variable
This variable is only accessible if you have the Relevanssi plugin installed and activated.
Relevanssi changes the excerpt output when using the main_query. Relevanssi then highlights the part of your search that matches with the posts. In order to display this, you will need to use the {{relevanssi}}
parameter instead of the {{item.preview(50)}}
or {{item.content|truncate(50)}}
methods mentioned earlier.
{{ relevanssi[item.ID].title }}
gets the highlighted post_title when matched.{{ relevanssi[item.ID].excerpt }}
gets the highlighted excerpt when matched.{{ relevanssi[item.ID].tags }}
gets the list of matching tags, and then highlights your search.{{ relevanssi['didyoumean'] }}
shows the Relevanssi ‘Did you mean: [alternative]’ suggestion.{{ relevanssi['didyoumean_link'] }}
shows the Relevanssi ‘[alternative]’ suggestion only. This way you can style it in your template.
Here’s an example using the relevansii variable:
{% if relevanssi.didyoumean %}<h5>{{relevanssi.didyoumean}}</h5>{% endif %}
{% for item in posts %}
<a href="{{item.link}}"><h3>{{relevanssi[item.ID].title}}</h3></a>
<p>{{relevanssi[item.ID].excerpt}}</p>
<p><a href="{{item.link}}">{{__('Read More')}}</a></p>
{% endfor %}
There are 3 filters for the relevanssi[ ‘didyoumean’ ] variable, to style the text before and after globally. Please refer to the documentation for its use.