<p>I've managed to get things working but I'm not sure if there is a better way of doing this. Any feedback is welcome.</p>

<p>Here's the new code.</p>

<pre><code>    {# Grab all the photo entries #}
    {% set photos = entry.photoGallery.type( 'photos' ) %}

    {# create a blank array to merge content #}
    {% set selectedFeaturedImages = [] %}

    {% for block in photos %}
        {# Add Featured Images to an array #}
        {% if block.imageLocation.contains('featured') %}
            {% set selectedFeaturedImages = selectedFeaturedImages|merge([block]) %}
        {% endif %}
    {% endfor %}

    {% for block in selectedFeaturedImages %}
        {% if loop.first %}
        &lt;div class="row" &gt;
            &lt;div class="columns"&gt;
                &lt;div class="slidr"&gt;
        {% endif %}

            {% for photo in block.photo %}
        &lt;div class="figure"&gt;&lt;a href="{{ photo.getUrl }}"&gt;&lt;img src="{{ photo.getUrl('projectHero') }}" alt=""&gt;&lt;/a&gt;
            {# Get the caption #}
            {% if block.caption|length %}
            &lt;div class="caption"&gt;{{ block.caption }}&lt;/div&gt;
            {% endif %}
        &lt;/div&gt;
            {% endfor %}

        {% if loop.last %}
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        {% endif %}
    {% endfor %}
</code></pre>

CC BY-SA 3.0
2017-01-06T01:40:49.640Z
I've marked this as the correct answer as I haven't had any feedback from anyone else and it is functioning properly.
Rheal Poirier
CC BY-SA 3.0
2017-01-03T19:49:41.103Z
2017-01-03T19:49:41.103Z
Rheal Poirier
<pre><code>{% if block.checkboxFieldHandle.contains('someChkbxValue') %}
&lt;p&gt;Executed!&lt;/p&gt;
{% endif %}
</code></pre>

<p>actual for Craft v2.x and v3.x</p>

CC BY-SA 4.0
2018-06-06T06:27:13.700Z
2018-06-06T06:27:13.700Z
Nikolay Bronskiy
<p>It looks as though you have the following line wrong:</p>

<p><code>{% for block in photos if block.imageLocation.contains('featured') %}</code></p>

<p>You will need to use the field name you're using to filter the Matrix block in place of <code>contains</code>. The following line will only loop through block with the <code>imageLocation</code> field set to "featured".</p>

<p><code>{% for block in photos.imageLocation('featured') %}</code></p>

CC BY-SA 3.0
2017-01-03T17:03:48.777Z
I had tried tried what you suggested but that gives all of the entries regardless if 'featured' is checked.
Rheal Poirier
CC BY-SA 3.0
2017-01-03T17:04:40.417Z
What is the error?
Luke Pearce
CC BY-SA 3.0
2017-01-03T17:10:23.757Z
I see in the craft docs that "you can tell if the option is selected or not via `option.selected`." but I'm unsure how to make that work in the if statement.

Like I mentioned in the original post. The above code works but I want to be able to hide the divs if there are no featured images.
Rheal Poirier
CC BY-SA 3.0
2017-01-03T11:37:25.633Z
2017-01-03T11:37:25.633Z
Luke Pearce
<p>I currently have checkboxes in a matrix block that allow a user to select where an image will be placed in the layout (as a featured slider image and/or in an image gallery). I've gotten the slider image to function but if, for some reason, there are no featured images I have a couple of unnecessary <code>&lt;div&gt;</code> tags. Because I am selecting only featured images from the matrix block I can not use <code>loop.first</code> and <code>loop.last</code> to move those divs in my for loop. </p>

<p>I had seen an article that seems close to finding the answer to my problem here : <a href="https://craftcms.stackexchange.com/questions/8915/only-execute-twig-block-if-a-matrix-radio-has-a-certain-value">Only execute twig block if a Matrix radio has a certain value</a> but I haven't been able to make it work. </p>

<p>Any help would be greatly appreciated.</p>

<pre><code>{# Add Feature Images - Slideshow #}
    {% set photos = entry.photoGallery.type( 'photos' ) %}
    {% if photos|length %}
    &lt;div class="row" &gt;
            &lt;div class="slidr"&gt;
                {% for block in photos if block.imageLocation.contains('featured')%}
                    {# Get the photo #}
                    {% for photo in block.photo %}
                      &lt;div class="figure"&gt;
                        &lt;a href="{{ photo.getUrl }}"&gt;
                          &lt;img src="{{ photo.getUrl('projectHero') }}" alt=""&gt;
                        &lt;/a&gt;
                        {# Get the caption #}
                        {% if block.caption|length %}
                          &lt;div class="caption"&gt;{{ block.caption }}&lt;/div&gt;
                        {% endif %}
                      &lt;/div&gt;
                    {% endfor %}
                {% endfor %}
            &lt;/div&gt;
    &lt;/div&gt;
    {% endif %}
</code></pre>

<p>I've attached a screen snapshot of my field setup.
<a href="https://i.sstatic.net/hXE8O.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/hXE8O.png" alt="screen snapshot of matrix field"></a></p>

<matrix><checkboxes>
Only executing if a matrix checkbox has a specific value
# Only executing if a matrix checkbox has a specific value

(Asked by: username_0 on 2017-01-03)

I currently have checkboxes in a matrix block that allow a user to select where an image will be placed in the layout (as a featured slider image and/or in an image gallery). I've gotten the slider image to function but if, for some reason, there are no featured images I have a couple of unnecessary  tags. Because I am selecting only featured images from the matrix block I can not use loop.first and loop.last to move those divs in my for loop. 
I had seen an article that seems close to finding the answer to my problem here : Only execute twig block if a Matrix radio has a certain value but I haven't been able to make it work. 
Any help would be greatly appreciated.
{# Add Feature Images - Slideshow #}
    {% set photos = entry.photoGallery.type( 'photos' ) %}
    {% if photos|length %}
    
            
                {% for block in photos if block.imageLocation.contains('featured')%}
                    {# Get the photo #}
                    {% for photo in block.photo %}
                      
                        
                          
                        
                        {# Get the caption #}
                        {% if block.caption|length %}
                          {{ block.caption }}
                        {% endif %}
                      
                    {% endfor %}
                {% endfor %}
            
    
    {% endif %}

I've attached a screen snapshot of my field setup.

**Tags:** matrix, checkboxes

---

## 3 answers

### Answer by username_0 on 2017-01-03. Score: 1 | Accepted answer

I've managed to get things working but I'm not sure if there is a better way of doing this. Any feedback is welcome.
Here's the new code.
    {# Grab all the photo entries #}
    {% set photos = entry.photoGallery.type( 'photos' ) %}

    {# create a blank array to merge content #}
    {% set selectedFeaturedImages = [] %}

    {% for block in photos %}
        {# Add Featured Images to an array #}
        {% if block.imageLocation.contains('featured') %}
            {% set selectedFeaturedImages = selectedFeaturedImages|merge([block]) %}
        {% endif %}
    {% endfor %}

    {% for block in selectedFeaturedImages %}
        {% if loop.first %}
        
            
                
        {% endif %}

            {% for photo in block.photo %}
        
            {# Get the caption #}
            {% if block.caption|length %}
            {{ block.caption }}
            {% endif %}
        
            {% endfor %}

        {% if loop.last %}
                
            
        
        {% endif %}
    {% endfor %}

> **Comments:**
> **username_0**: I've marked this as the correct answer as I haven't had any feedback from anyone else and it is functioning properly.  

---

### Answer by username_1 on 2018-06-06. Score: 3

{% if block.checkboxFieldHandle.contains('someChkbxValue') %}
Executed!
{% endif %}

actual for Craft v2.x and v3.x


---

### Answer by username_2 on 2017-01-03. Score: 2

It looks as though you have the following line wrong:

`{% for block in photos if block.imageLocation.contains('featured') %}`

You will need to use the field name you're using to filter the Matrix block in place of `contains`. The following line will only loop through block with the `imageLocation` field set to "featured".

`{% for block in photos.imageLocation('featured') %}`

> **Comments:**
> **username_0**: I had tried tried what you suggested but that gives all of the entries regardless if 'featured' is checked.  
> **username_2**: What is the error?  
> **username_0**: I see in the craft docs that "you can tell if the option is selected or not via `option.selected`." but I'm unsure how to make that work in the if statement.  Like I mentioned in the original post. The above code works but I want to be able to hide the divs if there are no featured images.  

---

