<p>Thanks to Brad for asking me a question above, it made me realize I was checking for matrix fields and not the matrix block. My code is now working that I am checking for the correct element on the page.  </p>

<p>Revised code:</p>

<pre><code>{% if entry.modelIds|length == 1 %}
     I am link - {{block.modelId}} &lt;br&gt;
{% else %}
     Not a Link - {{block.modelId}}&lt;br&gt;
{% endif %}
</code></pre>

<p>Thanks Brad for the help! </p>

CC BY-SA 3.0
2016-03-04T15:22:58.683Z
2016-03-04T15:22:58.683Z
johnHoysa
<p>I have a Matrix field which contains a bunch of blocks. If only one matrix field is present in the entry I need it to show a message. If more then one matrix field show an other message.</p>

<p>Below is my current code.</p>

<pre><code>{% for block in entry.modelIds.limit(1) %}
    {% if block.modelId|length == 1 %}
        I am link - {{block.modelId}} &lt;br&gt;
    {% else %}
        Not a Link - {{block.modelId}}&lt;br&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>Any help would be appreciated. I have tried a few different approaches and am just stumped. </p>

<p>Thanks!</p>

<p>Edit: adding image of fields.
<a href="https://i.sstatic.net/aQfs5.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/aQfs5.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2016-03-03T20:13:16.903Z
"If only one matrix field is present" Do you mean "matrix block" there? I assume `modelIds` is the Matrix field, what type of field is `modelId`?
Brad Bell
CC BY-SA 3.0
2016-03-03T21:07:11.070Z
modelIds is the matrix field name.  block.modelId is a plain text field within modelIds. I hope this helps.
johnHoysa
CC BY-SA 3.0
2016-03-04T00:06:38.573Z
So correct me if I'm wrong: if a Matrix block has 1 field show message A, if it has more than one field show message B?
Brad Bell
CC BY-SA 3.0
2016-03-04T14:54:03.790Z
That is/was correct. Now that I am thinking about it, what I want to check for is if there is only one modelIds per entry not individual modelId. I need to update my example code! Thanks for helping me figure out that I was testing for the wrong element. Update coming soon.
johnHoysa
<matrix><conditional>
If only one Matrix block show this, for more then one block show that
# If only one Matrix block show this, for more then one block show that

(Asked by: username_0 on 2016-03-03)

I have a Matrix field which contains a bunch of blocks. If only one matrix field is present in the entry I need it to show a message. If more then one matrix field show an other message.
Below is my current code.
{% for block in entry.modelIds.limit(1) %}
    {% if block.modelId|length == 1 %}
        I am link - {{block.modelId}} 
    {% else %}
        Not a Link - {{block.modelId}}
    {% endif %}
{% endfor %}

Any help would be appreciated. I have tried a few different approaches and am just stumped. 
Thanks!
Edit: adding image of fields.

**Tags:** matrix, conditional

> **Comments:**
> **username_1**: "If only one matrix field is present" Do you mean "matrix block" there? I assume `modelIds` is the Matrix field, what type of field is `modelId`?  
> **username_0**: modelIds is the matrix field name.  block.modelId is a plain text field within modelIds. I hope this helps.  
> **username_1**: So correct me if I'm wrong: if a Matrix block has 1 field show message A, if it has more than one field show message B?  
> **username_0**: That is/was correct. Now that I am thinking about it, what I want to check for is if there is only one modelIds per entry not individual modelId. I need to update my example code! Thanks for helping me figure out that I was testing for the wrong element. Update coming soon.  

---

## 1 answers

### Answer by username_0 on 2016-03-04. Score: 4 | Accepted answer

Thanks to Brad for asking me a question above, it made me realize I was checking for matrix fields and not the matrix block. My code is now working that I am checking for the correct element on the page.  
Revised code:
{% if entry.modelIds|length == 1 %}
     I am link - {{block.modelId}} 
{% else %}
     Not a Link - {{block.modelId}}
{% endif %}

Thanks Brad for the help!


---

