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}}
<ul>
{% set subCategories = craft.categories.descendantOf(category) %}
{% for subCat in subCategories %}
<li><a href="{{subCat.url}}#anchor"><img src="https://i.stack.imgur.com/zwipu.png"></a>
<a href="{{subCat.url}}#anchor" class="meta">{{subCat.title}}</a>
</li>
{% endfor %}
</ul>
{% 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}}
<ul>
{% 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() %}
<li><a href="{{subCat.url}}#anchor"><img src="{{image.getUrl()}}"></a>
<a href="{{subCat.url}}#anchor" class="meta">{{subCat.title}}</a>
</li>
{% endfor %}
</ul>
{% endfor %}