1

I have a channel of entries I'd like to loop through by order of importance on a scale of 1-5 with 5 being the most important and appearing first.

I tried to achieve this using a radio button field called importance and if else statements like:

{# List most important entries first #}
{% if entry.importance == "5" %}

    <li class="deal">
        <div class="brand-image-wrapper">
            {% if entry.logo | length %}
                {% for asset in entry.logo %}
                    <img class="grayscale" src="{{ asset.url }}" alt="{{ image.title }}">
                {% endfor %}
            {% endif %}
        </div>

        <div class="info">
            <h2 class="title">{{ entry.companyName }}</h2>
            <p>{{ entry.description }}</p>
        </div>
    </li>

    {% elseif entry.importance == "4" %}

        <li class="deal">
            <div class="brand-image-wrapper">
                {% if entry.logo | length %}
                    {% for asset in entry.logo %}
                        <img class="grayscale" src="{{ asset.url }}" alt="{{ image.title }}">
                    {% endfor %}
                {% endif %}
            </div>

            <div class="info">
                <h2 class="title">{{ entry.companyName }}</h2>
                <p>{{ entry.description }}</p>
            </div>
        </li>

        etc...

But this isn't very DRY and this lists the entries but they are still ordered by date. Is it possible to use .order with a field to achieve this?

4

Yes, you can use .order with any custom field, an example is below.

craft.entries.section( 'sectionHandle' ).order( 'customFieldHandle' )

For more info see the docs https://craftcms.com/docs/templating/craft.entries#order

|improve this answer|||||
  • I've tried .order('importance') but that shows the order from 1-5. So I tried .order('importance desc') which does show from 5-1 but also reverses the order showing the oldest entries first which I don't want. Is there a way to have 5-1 and newest first? Thanks – patrick.altair Apr 28 '16 at 10:17
  • 3
    You can sort by multiple fields in asc/desc order using .order( 'importance desc', 'postDate desc' ) – Luke Pearce Apr 28 '16 at 10:24

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.