<p>I would add the brand category field to the assets field set. This way you can add brands to images and use <code>.relatedTo</code> to find them.</p>

<pre><code>&lt;ul&gt;
  {% for category in craft.categories.group( 'brands' ) %}
    // This will give you the first image that relates to the category, you could use .find() to get all images and then loop through them.
    {% set image = craft.assets.relatedTo( category ).first() %}

    &lt;li&gt;
      &lt;a href="{{ category.url }}"&gt;
        {% if image|length %}
          &lt;img src="{{ image.url }}" /&gt;
        {% else %}
          &lt;img src="http://placehold.it/400x300" /&gt;
        {% endif %}
      &lt;/a&gt;
    &lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;
</code></pre>

CC BY-SA 3.0
2016-05-21T09:05:26.017Z
Perfect. Got what I needed and learned something new. Thank you Luke!
Kitsune
CC BY-SA 3.0
2016-05-22T09:36:35.550Z
@kitsune what if you'd just add an asset field "logo" to the categories? That makes it possible to use the more convenient syntax `myCategory.logo.first()` to get a category's brand logo. Makes more sense from the data entry aswell, add logo, address, etc. to the brand category.
carlcs
CC BY-SA 3.0
2016-05-22T10:37:40.153Z
I'd implement my way if there are many images related to a category but go with carlcs way if you only need a single image to be associated with a category.
Luke Pearce
CC BY-SA 3.0
2016-05-21T08:53:36.327Z
2016-05-21T09:22:42.077Z
2016-05-21T09:22:42.077Z
Luke Pearce
<p>What would be the best approach to have a list of categories (brands in my case.) and if an asset (image) exists that matches that brand name show that asset or instead show a placeholder image.</p>

<pre><code>&lt;ul&gt;
{% for category in craft.categories.group('brands') %}
    &lt;li&gt;
        &lt;a href="{{ category.url }}"&gt;
            &lt;img src="{{ .. }}" /&gt;
        &lt;/a&gt;
    &lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
</code></pre>

CC BY-SA 3.0
2016-05-21T08:28:10.557Z
So with something along the lines of;

`{% for image in craft.assets.kind('brands') %}
     <li>
        <a href="{{ category.url }}">
            <img src="{{ image.getUrl() }}" />
        </a>
    </li></li>
{% endfor %}`

But with an if statement there too, if it's exist use it, else a placeholder url for the src.
Kitsune
<assets><categories>
Find matching asset (image) for category
# Find matching asset (image) for category

(Asked by: username_0 on 2016-05-21)

What would be the best approach to have a list of categories (brands in my case.) and if an asset (image) exists that matches that brand name show that asset or instead show a placeholder image.

{% for category in craft.categories.group('brands') %}
    
        
            
        
    
{% endfor %}

**Tags:** assets, categories

> **Comments:**
> **username_0**: So with something along the lines of;  `{% for image in craft.assets.kind('brands') %}      <li>         <a href="{{ category.url }}">             <img src="{{ image.getUrl() }}" />         </a>     </li></li> {% endfor %}`  But with an if statement there too, if it's exist use it, else a placeholder url for the src.  

---

## 1 answers

### Answer by username_1 on 2016-05-21. Score: 4 | Accepted answer

I would add the brand category field to the assets field set. This way you can add brands to images and use .relatedTo to find them.

  {% for category in craft.categories.group( 'brands' ) %}
    // This will give you the first image that relates to the category, you could use .find() to get all images and then loop through them.
    {% set image = craft.assets.relatedTo( category ).first() %}

    
      
        {% if image|length %}
          
        {% else %}
          
        {% endif %}
      
    
  {% endfor %}

> **Comments:**
> **username_0**: Perfect. Got what I needed and learned something new. Thank you Luke!  
> **username_2**: @kitsune what if you'd just add an asset field "logo" to the categories? That makes it possible to use the more convenient syntax `myCategory.logo.first()` to get a category's brand logo. Makes more sense from the data entry aswell, add logo, address, etc. to the brand category.  
> **username_1**: I'd implement my way if there are many images related to a category but go with username_2 way if you only need a single image to be associated with a category.  

---

