0

I have an image grid for my articles, and its sorted by the type of articles. So all the articles of a certain type is all put in the same item grid, but I want to sort it after the brand aswell by having my image grid sort under a logo of the brand. So if I have 2 different brands of 1 type of article, I want 2 image grids on the same page, neatly sorted under their respective logo.

This is what I have so far:

<div class="widewrapper weak-highlight">
<div class="container content">       
    {% for entry in entry.children %}
    {% if entry.type=="lift" and entry.liftMerke=="nifty" %}
    <div class="image">
        <img class="img-responsive" src="/uploads/images/logo-brand1" alt="">
    </div>
    <div class="row">
        {% for entry in entry.children %}
        {% if entry.type=="lift" and entry.liftMerke=="nifty" %}               
        <div class="showroom-item col-sm-4 col-md-3 col-lg-3">
            <div class="image">
                {% set bilde = entry.bilde.one() %}
                {% if bilde %}
                <img class="img-responsive" src="{{ bilde.geturl('fremsideKnapper') }}" alt="">
                {% endif %}
                <a class="overlay" href="{{ entry.url }}">
                     <i class="glyphicons search"></i>
                     <div class="name"><p>{{ entry.title }}</p><p>Arbeidshøyde: {{ entry.arbeidshoyde }}</p></div>
                </a>
                <a 
                    href="{{ entry.url }}" 
                    class="btn btn-default btn-gantic btn-xs btn-block">
                    <h3>{{ entry.title }} | {{ entry.arbeidshoyde }}</h3>
                </a>
            </div>
        </div>                    
       {% endif %}
       {% endfor %}            
    </div>
    {% endif %}
    {% endfor %}  
    <div class="clearfix"></div>
</div>

So this gives me just a whole bunch of logos whitch is not what I want. Is there something I can do to make it search through the entry.children articles, and do something only once(print logo) if something is true for either one of the articles? Really appreciate any help!

EDIT:

So I have been playing around with this abit now, but I cant really get it to work. I forgott to mention something inportant in my post aswell, I am using craft3 beta.

So I am abit unsure if the errors I am encountering are me not really understanding what I am doing or if it is related to differences between craft 2 and 3.

I created a catogory group(liftmerker) and two catogories(denka, nifty). I added a image field(image field is called "bilde") to the catogories where I uploaded the logos.

This is what the code looks like at the monent:

<div class="widewrapper weak-highlight">
<div class="container content">       
    {% set entries = entry.children | group( 'entryCategory.one().title' ) %}
    {% set brands = craft.categories.group( 'liftmerker' ).titles %}
    {% for brand in brands %}
        {% set brandEntries = entries[ '' ~ category ] | default( [] ) %}
        {% if brandEntries | length %}
        <div class="image">
            <img class="img-responsive" src="{{ brand.bilde.first.url }}" alt="{{ brand.first.title }}">
        </div>

    <div class="row">
            {% for entry in brandEntries %}
                ...
            {% endfor %}        
    </div>
        {% endif %}
    <div class="clearfix"></div>
    {% endfor %}
</div>

This gives me an error that I dont really understand:

TypeError
Argument 1 passed to craft\web\twig\Extension::groupFilter() must be of the type array, object given, called in /home/vagrant/Code/Gantic/Gantic3/storage/runtime/compiled_templates/c3/c383e9d48f1c478076ea5c1b97037f5aa9b4242fb3b6dcb877a0488b0b8b32b7.php on line 114

Any suggestions for what I can do different or what line in the code is causing the error?

1
  • I just voted to close this. Craft Stack Exchange is not a good place to post Craft beta issues. Please post this on github.com/craftcms/cms or start a discussion in the #craft3 channel in Craft Slack.
    – carlcs
    May 3, 2017 at 11:13

1 Answer 1

2

I hope I got your issue right. For the brand I'd use a category. So each article can be assigned to a category brand.

You can then group your entries by category:

{# Save entries, group by category #}
{% set entries = entry.children | group( 'entryCategory.first().title' ) %}

{# Get all category logos for the category group "yourEntryCategoryHandle" #}
{% set brands = craft.categories.group( 'yourBrandCategoryHandle' ).titles %}

{# Loop through the category title array #}
{% for brand in brands %}

    {# Get the entries grouped under the current brand title #}
    {% set brandEntries = entries[ '' ~ category ] | default( [] ) %}

    {# Print the category title and entries #}
    {% if brandEntries | length %}
    <div class="image">
        <img class="img-responsive" src="{{ brand.image.first.url }}" alt="{{ brand.image.first.title }}">
    </div>
    <div class="row">
        {% for entry in brandEntries %}
            ...
        {% endfor %}
    </div>
    {% endif %}

{% endfor %}

This is a modification from this issue: How Do I group entries by Category and retain fixed Category Order?

2
  • I have not used categories before, but this looks very promising :) thanks for the reply! will get back to you if this solves it when I have figured out how the catogories work.
    – laden
    May 3, 2017 at 8:13
  • Sure, actually they work pretty much the same as channels/structures do. They help you relate content to certain general categorizations. So, in this case you can create an asset field that contains the brand and use the brand name as title and you're good to go. Then create a category field and add it to your product/entry to be able to select the category. But as @carlcs wrote, better place to discuss this is Craft Slack channel – its community is great and very helping. May 4, 2017 at 9:05

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