Is it possible to output only the first word of the title of an entry in a template? If so, how? Thanks!
2 Answers
Yes, you can do this with Twig alone. Here's how:
{{ entry.title|split(' ')|slice(0, 1)|join }}
This series of Twig filters:
- Uses
split
to turn the title into an array, splitting on spaces. - Passes that array through
slice
to grab the first item in the array. - Then uses
join
to turn the array back into a string for output. The array is now just one item (the first word of the title), butjoin
is needed because Twig doesn't want to treat an array as a string.
There is also a nice plugin called Hacksaw that gives you a handful of helpers. With this you can do {{ entry.title|hack(words='1') }}