3

I have a list of images that are loaded directly from the content of a specific asset folder.

{% for image in craft.assets.find({ folderId: 18, limit: 240, order: filename }) %}

What I want to achieve is that one (or more) images from that assets folder are listed first (second etc.) before the actual ordering by filename starts place. Much in the same way as sticky entries (EE).

5

Add a lightswitch field to your assets and query for assets with the lightswitch enabled first. Use the ids() method to get just the assets' IDs and merge them with the rest of your assets' IDs to have them all in your desired sort order.

You can now query for the assets models by passing that IDs array to the id parameter and adding fixedOrder(true).

{% set stickyImagesIds = craft.assets({
    folderId: 18,
    order: filename
    sticky: true
}).ids() %}

{% set regularImagesIds = craft.assets({
    folderId: 18,
    order: filename
}).ids() %}

{% set allImagesIds = stickyImagesIds|merge(
    regularImagesIds|without(stickyImagesIds)
) %}

{% set allImages = craft.assets.id(allImagesIds).fixedOrder(true).limit(240) %}
8

Based on adding a lightswicht ("sticky") as suggested by @carlcs I think you should be able to solve this by just using two order fields.

{% set images = craft.assets.folderId(18).order('sticky desc, filename asc').limit(240); %}

You should be able to save some twig code, maybe even requests. Haven't tested this though.

  • carlcs and pascal's suggestion combined did the trick meanwhile saving some twig code. Both brilliant and simple ;-) – Robbie Post Sep 2 '15 at 15:18

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.