2

This is a news feed with a title, image and short blurb beneath. The entry content is built with the Matrix. An entry can be made up of a single 'textEditorBlock' or multiple 'textEditorBlock'.

I just want to return the first 'textEditorBlock' from Matrix. Currently, all existing 'textEditorBlock' blocks get returned.

{# Count the number of Matrix Blocks; type: textEditorBlock  #}
{% set blocks = post.buildStory.type('textEditorBlock') %}
{{ blocks | length }}

{% for block in post.buildStory.all() %}
   {# the text #}
   {% set paragraphs = block.textEditor %}
   {% set teaser = paragraphs | length > 75 ? paragraphs | slice(0, 250) ~ '...' :  paragraphs %}
   <p>{{  teaser  | striptags | raw | typogrify  }}</p>       
{% endfor %} 

Note: the {{ blocks | length }} tells me the number of 'textEditorBlock' that exist for each entry (1, 4, 4, 2 and 2).

4

You could try something like this:

{% set block = post.buildStory.type('textEditorBlock').first() %}
    {{block.textEditor}}
| improve this answer | |
  • That's it! Solution: {% set blocks = entry.buildStory.type('textEditorBlock').one() %} {% set paragraphs = blocks.textEditor %} {% set teaser = paragraphs | length > 75 ? paragraphs | slice(0, 250) ~ '...' : paragraphs %} <p>{{ teaser | striptags | raw | typogrify }}</p> – TRIM Nov 8 '17 at 17:11
  • Glad you got it working :) – alexr Nov 8 '17 at 17:18
1

You could do:

{% set blocks = post.buildStory.type('textEditorBlock').limit(1) %}

Also, not your question, but this hacksaw port is great for those teasers. Has some extra logic to make those a bit cleaner. Check it out.

https://github.com/ehousestudio/craft_hacksaw

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.