<p>This should work!</p>

<p>This code is to pull the first image, from the first entry of the first sub category given that the 'slug' passed into the first set is your top level category.</p>

<pre><code>    {# Get the category from the slug. This will return one CategoryModel #}
    {% set topCat = craft.categories.slug('someSlug').first() %}

    {# Get the first subcategory under the top level category using the getChildren() method on the category model.  #}
    {% set firstSubCat = topCat.getChildren().first() %}

    {# Grab the first entry under that category using the relatedTo function and appending the .first() method to return only one item. #}
    {% set firstEntry = craft.entries.relatedTo( firstSubCat ).first()  %}

    {# Set your image from your asset field #}
    {% set image = firstEntry.assetField.first() %}
</code></pre>

CC BY-SA 3.0
2014-07-16T18:51:23.483Z
As someone still trying to grasp the syntax of Craft and Twig, your comments really help break down what's happening on each line. For my specific use case, I need to use my edited code (with the help of Marion's tip), but your answer is great for the question I asked. Thanks so much!
Steph
CC BY-SA 3.0
2014-07-16T18:55:36.650Z
No prob! Glad you got it worked out! :)
Ian
CC BY-SA 3.0
2014-07-16T18:40:19.723Z
2014-07-16T18:40:19.723Z
Ian
<p>I just want to apologize in advance if I'm not quite understanding how the element criteria model works. I know that's the basis of Craft but I learn best by doing and this StackExchange has been an incredible resource. Thank you!</p>
<p>As my question title explains, I'm trying to get an image (image url actually) located in an asset field from the first entry of the first subcategory within a category.</p>
<p>This is what I have so far:</p>
<pre><code>    {% set lastSeg = craft.request.lastSegment %}
    {% for cat in categories.slug(lastSeg) %}
      {{category.title}}
      {{category.catContent}}
      &lt;ul&gt;
        {% set subCategories = craft.categories.descendantOf(category) %}
        {% for subCat in subCategories %}

        &lt;li&gt;&lt;a href=&quot;{{subCat.url}}#anchor&quot;&gt;&lt;img src=&quot;https://i.sstatic.net/zwipu.png&quot;&gt;&lt;/a&gt;
            &lt;a href=&quot;{{subCat.url}}#anchor&quot; class=&quot;meta&quot;&gt;{{subCat.title}}&lt;/a&gt;
        &lt;/li&gt;

        {% endfor %}
       &lt;/ul&gt;
    {% endfor %}
</code></pre>
<p>I'd like the image url to replace <a href="http://fpoimg.com/280x280" rel="nofollow noreferrer">http://fpoimg.com/280x280</a> as the img src. I'm getting stuck after looping through the subcategories. I'm not sure how to loop through entries in the subcategory and return the first one, so that I can then loop through the assets and pull the image url I need.</p>
<p>-- edit --</p>
<p>I think I have gotten it to work thanks to Marion's comment, but I would love some input to be certain:</p>
<pre><code>    {% set lastSeg = craft.request.lastSegment %}
    {% for cat in categories.slug(lastSeg) %}
      {{category.title}}
      {{category.catContent}}
      &lt;ul&gt;
        {% set subCategories = craft.categories.descendantOf(category) %}
        {% set products = craft.entries.section('products').relatedTo(subCategories) %}

        {% for subCat in subCategories %}
        {% for entry in products.relatedTo(subCat).limit(1) %}
        {% set image = entry.productImage.first() %}

        &lt;li&gt;&lt;a href=&quot;{{subCat.url}}#anchor&quot;&gt;&lt;img src=&quot;{{image.getUrl()}}&quot;&gt;&lt;/a&gt;
            &lt;a href=&quot;{{subCat.url}}#anchor&quot; class=&quot;meta&quot;&gt;{{subCat.title}}&lt;/a&gt;
        &lt;/li&gt;

        {% endfor %}
       &lt;/ul&gt;
    {% endfor %}
</code></pre>

CC BY-SA 3.0
2014-07-16T17:03:11.840Z
By *entry in the subcategory* do you mean entry **relatedTo** the subcategory?
Marion Newlevant
CC BY-SA 3.0
2014-07-16T17:50:37.407Z
Yes, that's what I actually meant... and thank you for suggesting it because I can see how I was thinking about it all wrong. Haha, baby steps for me.
Steph
<assets>
How do I pull the first image from the first entry of the first subcategory?
# How do I pull the first image from the first entry of the first subcategory?

(Asked by: username_0 on 2014-07-16)

I just want to apologize in advance if I'm not quite understanding how the element criteria model works. I know that's the basis of Craft but I learn best by doing and this StackExchange has been an incredible resource. Thank you!
As my question title explains, I'm trying to get an image (image url actually) located in an asset field from the first entry of the first subcategory within a category.
This is what I have so far:
    {% set lastSeg = craft.request.lastSegment %}
    {% for cat in categories.slug(lastSeg) %}
      {{category.title}}
      {{category.catContent}}
      
        {% set subCategories = craft.categories.descendantOf(category) %}
        {% for subCat in subCategories %}

        
            {{subCat.title}}
        

        {% endfor %}
       
    {% endfor %}

I'd like the image url to replace http://fpoimg.com/280x280 as the img src. I'm getting stuck after looping through the subcategories. I'm not sure how to loop through entries in the subcategory and return the first one, so that I can then loop through the assets and pull the image url I need.
-- edit --
I think I have gotten it to work thanks to Marion's comment, but I would love some input to be certain:
    {% set lastSeg = craft.request.lastSegment %}
    {% for cat in categories.slug(lastSeg) %}
      {{category.title}}
      {{category.catContent}}
      
        {% set subCategories = craft.categories.descendantOf(category) %}
        {% set products = craft.entries.section('products').relatedTo(subCategories) %}

        {% for subCat in subCategories %}
        {% for entry in products.relatedTo(subCat).limit(1) %}
        {% set image = entry.productImage.first() %}

        
            {{subCat.title}}
        

        {% endfor %}
       
    {% endfor %}

**Tags:** assets
> **Comments:**
> **username_1**: By *entry in the subcategory* do you mean entry **relatedTo** the subcategory?  
> **username_0**: Yes, that's what I actually meant... and thank you for suggesting it because I can see how I was thinking about it all wrong. Haha, baby steps for me.  

---

## 1 answers

### Answer by username_2 on 2014-07-16. Score: 5 | Accepted answer

This should work!
This code is to pull the first image, from the first entry of the first sub category given that the 'slug' passed into the first set is your top level category.
    {# Get the category from the slug. This will return one CategoryModel #}
    {% set topCat = craft.categories.slug('someSlug').first() %}

    {# Get the first subcategory under the top level category using the getChildren() method on the category model.  #}
    {% set firstSubCat = topCat.getChildren().first() %}

    {# Grab the first entry under that category using the relatedTo function and appending the .first() method to return only one item. #}
    {% set firstEntry = craft.entries.relatedTo( firstSubCat ).first()  %}

    {# Set your image from your asset field #}
    {% set image = firstEntry.assetField.first() %}

> **Comments:**
> **username_0**: As someone still trying to grasp the syntax of Craft and Twig, your comments really help break down what's happening on each line. For my specific use case, I need to use my edited code (with the help of Marion's tip), but your answer is great for the question I asked. Thanks so much!  
> **username_2**: No prob! Glad you got it worked out! :)  

---

