2

I have a bunch of images that aren't related to any entries but are in an asset source. Let's call this 'My Images'.

How do I just grab those assets? I can't find anything in the docs that suggests this is possible—it seems such an obvious use-case though I'm sure I am missing something.

4

What you want is craft.assets

You can access your site’s assets from your templates via craft.assets. It returns an ElementCriteriaModel object.

Use sourceId for the parameter.

To find the sourceId, look at the craft_assetsources table in the database. There doesn't seem to be any way to query for AssetSourceModel.

| improve this answer | |
  • Yes I saw this. It's pretty cryptic though, how do I find the sourceId? – hamishtaplin Sep 21 '14 at 12:05
  • 1
    Thanks, this works. Not a very intuitive solution though, I would hope there is a better way than having to look up id's in the database! – hamishtaplin Sep 22 '14 at 8:20
3

You can fetch assets related to a certain source by using the craft.assets.source method and passing in the source handle.

Assuming your handle is 'my_images', the following will do the job:

craft.assets.source('my_images')

You avoid having to lookup db IDs that way.

| improve this answer | |
  • 3
    Note: If you want to pull all Assets, be sure to set the limit parameter to null, i.e. {% set assets = craft.assets.source('my_images').limit(null) %}, otherwise your query will be limited to 100 Assets. – Mats Mikkel Rummelhoff Dec 5 '17 at 9: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.