Skip to content

Instantly share code, notes, and snippets.

@meredevelopment
Created November 6, 2019 11:45
Show Gist options
  • Save meredevelopment/67bb05d848b887a4e7a0816ba2d23a25 to your computer and use it in GitHub Desktop.
Save meredevelopment/67bb05d848b887a4e7a0816ba2d23a25 to your computer and use it in GitHub Desktop.
Using the t() function in Craft 3 CMS to show "# result" or "# results" based on count. Plurals.
{% set searchQuery = craft.app.request.getParam('q') %}
{% set entries = craft.entries()
.search(searchQuery)
.orderBy('score')
.all() %}
{% if entries|length %}
<p>
{{ '{resCount,plural,
one{# result}
other{# results}
} ' | t ({resCount: entries|length }) }}
</p>
<ul>
{% for entry in entries %}
<li>
<a href="{{ entry.url }}">{{ entry.title }}</a>
{# etc etc etc #}
</li>
{% endfor %}
</ul>
{% else %}
<p>Your search for “{{ searchQuery }}” didn’t return any results.</p>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment