Just installed the cms last night. I am looking for a step-by-step instruction on how to create a gallery and display it.

So far I have created the: - channel called: Gallery - section called: Gallery - entry type: Gallery - the assets field named: gallery - added 4 images - created a new entry called: Gallery (only the Title is visible so far) and this is where I got stuck.

Any help would be greatly appreciated


I'm a beginner myself but have used assets plenty of times. It should be something like this:

{% set entry = craft.entries.section('gallery').first() %}
{% set assets = entry.gallery %}
{% if assets | length %}
    {% for asset in assets %}
        <img src="{{ asset.getUrl('imgTransform') }}" width="{{ asset.getWidth('imgTransform') }}" height="{{ asset.getHeight('imgTransform') }}" alt="{{ entry.title }}">
    {% endfor %}
{% endif %}

I use a set entry instead of a for loop because I'm assuming you only have one entry (one gallery). Maybe use a single section instead of a channel than? If you have multiple galleries, a channel is perfect, than you can use a for loop.

The imgTransform can be set in your assets page in the Control Panel. More info: http://buildwithcraft.com/docs/image-transforms

  • So where do you put this code? In a new template? I am really new to this. A one-gallery solution is perfectly fine. Could you please give me detailed instruction for that? thanks
    – Csaba Dobo
    Nov 16 '14 at 11:57
  • Yes. Just a HTML file that you reference to in your channel settings. For example gallery.html. You can put the name of your template into the bottom input field of your channel settings in the CP. Than when you go to yoursite.com/gallery it'll load the template and output your gallery. Take a look at github.com/pixelandtonic/HappyLager/tree/master/final/web for some examples. Nov 16 '14 at 12:04
  • @TomDeSmet You had an extra endfor at the end that I removed. I hope you don't mind the edit. ;) Nov 16 '14 at 13:33
  • Certainly not :) Will double check my code next time. Thanks! Nov 16 '14 at 13:34

I thought I might fill in a few extra details to support Tom De Smet's excellent answer.

If you only need one gallery then as Tom suggested you can change the section type from a channel to a single in the section's settings. Where you choose to display the gallery is completely up to you. It can either be displayed in it's own template (the one you define in the section's settings), or just as easily in any other template or page on your site.

If displayed in its own template, and if using a single then the template might be something like gallery.html or gallery/index.html, either of which could be accessed by the url mysite.com/gallery. If using a channel (with one or more gallery entries) then a common convention would be to use gallery/_entry.html, which could be accessed using the url mysite.com/gallery/my-gallery-slug.

Whenever a request is made to either of these uri patterns craft will automatically provide an entry variable to the template populated with an EntryModel for the gallery entry — in which case you do not need to retrieve the entry using {% set entry = craft.entries.section('gallery').first() %} as entry will already be pre-defined.

If on the other hand you want to display the gallery somewhere else on your site (in some other template) then you will first need to retrieve the gallery entry using {% set entry = craft.entries.section('gallery').first() %} or {% set entry = craft.entries.section('gallery').slug('my-gallery-slug') %}.

Also, keep in mind that using a structure with multiple EntryTypes defined (one of which could be 'gallery') would be another way to essentially house various 'singles' together with the advantage that you can then generate heirarchical site navigation from the structure.

  • Hello, many thanks for your answers. Let me ask this too: is the above code for single age type or for channels or for both?
    – Csaba Dobo
    Nov 16 '14 at 19:06
  • Most of the code on both answers will work for singles, channels, or structures. The difference is that in a channel or structure (where there can be more than one) you need to either loop through the entries or provide some additional identifier (i.e. id(), slug(), first(), etc.) to retrieve the one you are looking for. And if you're using the defined template path then entry will already be defined and you can just access it directly (i.e. {{ entry.title }} ). Nov 16 '14 at 19:19
  • I feel sorry for my beginner questions. I converted this setup to a single, inserted the code above to a gallery.html file but get this: "Internal Server Error Craft\EntryModel and its behaviours do not have a method or closure named "gallery"." In under Entries/singles/gallery I only have Title input field. Maybe that is why it is not working. I would post a screenshot but can not do that here.
    – Csaba Dobo
    Nov 16 '14 at 19:59
  • Ah yes. You need to add your field 'gallery' to the fieldLayout of your single, also called gallery. You can do this in settings>sections>gallery>entry type>gallery, then drag the field to your sections field layout. Nov 16 '14 at 20:30
  • It is getting better. Now I managed to add the assets to the single net to title. But now what I get is: "Internal Server Error The transform “imgTransform” cannot be found!" I had previously set up image transfer in the control panel, but no effect so I deleted it. Do I have to write something in the code in the gallery.html template? hm??
    – Csaba Dobo
    Nov 16 '14 at 20:53

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.