3

I am fairly new to Craft CMS coming from ruby on rails and angularjs. I am running into an issue with attempting to display images on a home page based on whether or not the radio button titled "featured" ( a field) is selected.

I have searched around but was unable to find something addressing this, or at least in a way I understood to solve the problem.

I have been trying variations of...

{% if thumbnails.featured %}

...hoping it would only display what has "featured" selected but it either breaks and shows nothing or shows all the images. Based on what I have found I think I have to "set" something but am unable to figure out exactly how that is set up. This is exactly what I have right now and it is showing all images.

{% for thumbnails in craft.assets.thumbnails %}
    {% if thumbnails.featured %}
        <div class="rollover-item wd ux dv print">              
            <img src="assets/img/{{ thumbnails.filename }}" alt="{{ thumbnails.filename }}">
                    <a class="preview" href="work/{{ thumbnails.clientname }}">{{ thumbnails.caption }}</a>                     
        </div>
    {% endif %}
{% endfor %}

If anyone could suggest a solution or even point me in the right direction it would be greatly appreciated.

1

You will want to first create an 'elementCriteriaModel' to retrieve only the 'featured' images from the 'thumbnails' asset group, which will return an array like object (not technically an array) which you can loop through to display.

{% set images = craft.assets.group('thumbnails').featured(1) %}
{% for image in images %}
    <div class="rollover-item wd ux dv print">              
        <img src="assets/img/{{ image.filename }}" alt="{{ image.filename }}">
        <a class="preview" href="work/{{ image.clientname }}">{{ image.caption }}</a>
    </div>
{% endfor %}

Another method that might be easier to manage from a client perspective is to add an assets fieldType to your homepage section (called 'featuredImages' or whatever; limited of course to the 'thumbnails' asset group) which the client can use to add and/or reorder the featured thumbnails to be displayed. In which case the code would look something more like this.

{% if entry.featuredImages|length %}
    {% for image in entry.featuredImages %}
        <div class="rollover-item wd ux dv print">              
            <img src="assets/img/{{ image.filename }}" alt="{{ image.filename }}">
            <a class="preview" href="work/{{ image.clientname }}">{{ image.caption }}</a>
        </div>
    {% endfor %}
{% endif %}
  • I tried the first option but it still would not show any of the thumbnails. I do not want to do the second because this grid that is used to display the thumbnails will be on one other page but not all the pages and not just that one, and while I do not understand craft all that well yet I do not want that as part of the template for the single pages. Perhaps there is an error on how I am saving the images or something, I will just have to keep at it. Thanks you. – Rockwell Rice Jun 25 '15 at 17:28
  • There may be an issue with the naming. Check that the asset group handle is called "thumbanils", that the radio button field handle is called "featured" and that the yes/featured value is "1" (or modify the code accordingly). If the value of the "featured" option is something else (like "featured") then use that value instead (i.e. craft.assets.group('thumbnails').featured('featured'). – Douglas McDonald Jun 25 '15 at 17:41
  • The advantage of using an assets field is that you can reorder the images, something that you won't be able to do by retrieving the assets directly. The other advantage is that you can later selectively feature the same image on more than one page if needed. Keep in mind that you can always retrieve the data from whatever entry it is stored on and use that data on other pages (just retrieve the entry and loop through the information as normal). You could even save this as a separate macro file or template include file to be used anywhere as needed. – Douglas McDonald Jun 25 '15 at 17:49
  • yes that was it, the value was set to "featured" instead of "1." I will check this off as answered, thanks much for your help. – Rockwell Rice Jun 25 '15 at 18:10
  • You might want to use a global assets field - then it wouldn't be tied to a specific page. – Marion Newlevant Jun 25 '15 at 18:26

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.