1

I have succussfully been able to list entries within the 'brandx' category slug, but I can't figure out a way to print specific content only if the current 'product' listing is related to the 'brandx' category.

I have tried quite a few things and have come up empty, so I feel like I'm either missing something basic, or there's a trick I can't find in the docs or here.

{% paginate craft.entries.section('shopDetails').level(1).limit(24) as postsOnPage %} 
      {% for product in postsOnPage %}
           …
            <h5>
              {% set cat = craft.categories.slug('brandx').first() %}
              {% set catGroup = craft.entries.relatedTo( cat ) %}
              {% if product {# is within the catGroup #}  %}
              Brand X
              {% endif %}
              {{ product.title }}
            </h5>
{% endpaginate %}
2

All you need to do is this in the product for Loop

{% if product.category.first.slug == 'brandx' %}
    Brand X
{% endif %}

Or Shorthand

{{ product.category.first.slug == 'brandx' ? 'Brand X' }}  

Not tested but thats the gist of it.

--Edited

3
  • Yep works with a couple of formatting edits. This solution is particularly annoying since my first gut was to just pull from the category field, but I didn't include the 'first'. I'm still unclear when 'first' is needed (time to research).
    – Ben
    Dec 17 '14 at 3:35
  • first is needed whenever the default returns an array (or an array-like object), you know there is only one, or you only want the first one. I.e. an assets, category, entries, or users field for example that you have set a limit of 1. Dec 17 '14 at 4:08
  • This is nice and tidy, but will not work if you have set multiple categories before brandx. I have used: {% set isBrandx = false %} {% for item in product.category %} {% if product.slug == 'brandx' %} {% set isBrandx = true %} {% endif %} {% endfor %} I can then test against isBrandx wherever I want. Open to suggestions, however!
    – ianp
    Sep 16 '17 at 18:07

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.