Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

Is there any way to output the content of a channel entry by their slug?

For example, say I have an asset field called "image" in each channel entry, I want to output the content of that field only for the channel entry filtered by the slug "home", I've tried both:

{% for item in craft.entries.section('images').slug('home') %}
    {{ item.image.first() }}
{% endfor %}

and

{% for item in craft.entries.section('images').find().slug('home') %}
    {{ item.image.first() }}
{% endfor %}

but neither work...

share|improve this question
1  
By "images" do you mean entries or asset-fields inside entries? Also your code looks correct, can you post a bit more of it? –  Victor In Jul 12 at 13:27
    
Hi Victor, I've edited the question to clarify what I mean. In short, say I have an asset field called "image" in each channel entry, I want to output the content of that field only for the channel entry filtered by the slug. –  negatron Jul 12 at 13:49

1 Answer 1

up vote 3 down vote accepted

The second example won't work since .find() should be at the end of the ElementCriteriaModel. But since you treat the CriteriaModel like an array, you don't need to add it at all.

Your first code looks correct, but you should check if the loop found an entry:

{% for item in craft.entries.section('images').slug('home') %}
    ....
{% else %}
    No entry found
{% endfor %}

Also your current code is only outputting the title of the AssetFileModel:

Outputting an AssetFileModel object without attaching a property or method will return the asset’s Title

That's why you should add the "url" property, and also check whether the field contains an asset:

{% for item in craft.entries.section('images').slug('home') %}
    {% set asset = item.image.first() %}

    {% if asset %}
       {{ asset.url }}
    {% else %}
        No asset added to the field
    {% endif %}
{% else %}
    No entry found
{% endfor %}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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