I'm trying to grab a specific image to display that is located in assets. I tried using {% set image = craft.assets.filename('image_filename.jpg') %}
but I get the error "An unknown image object was used." I also tried referring to one of the asset sources which I saw as a solution in another post related to my question: {% set image = craft.assets.source('my_images') %}
but I'm not sure how that would help me grab a specific image. {% set image = craft.assets.images[0] %}
just grabs an image from any of the asset sources if I change the index number. What am I doing wrong here?? Thanks in advance!
1 Answer
You can do
{% image = craft.assets.id(x).first() %}
Where x is the ID of your element. You can see the ids of your elements in your cp. There is a little wheel icon below your asset sources where you can define which attributes to display. Just check the selectbox for id
You can learn more about fetching entries with ElementCriteriaModels
in the docs
Your problem was: you did not fetch the element. You prepared your database query but you need to execute it with first()
or find()
because
-
I was just about to update this post saying that I figured out the solution with this very same method. Glad to know I did it right. Thanks!– MorganJan 17, 2018 at 18:50
-
1