2

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="http://fpoimg.com/280x280"></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 %}
  • 1
    By entry in the subcategory do you mean entry relatedTo the subcategory? – Marion Newlevant Jul 16 '14 at 17:03
  • 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 Jul 16 '14 at 17:50
5

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() %}
| improve this answer | |
  • 1
    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 Jul 16 '14 at 18:51
  • No prob! Glad you got it worked out! :) – Ian Jul 16 '14 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.