J Scott Smith

Listing entries from multiple folders with unique markup in Statamic

Articles - August 17th, 2014

For this site, I had planned to list both “articles” and “notes” as entries on the homepage, chronologically. Turns out it’s trickier than I thought to format each of the entries differently but I was able to sort it out. I’ll go over how it can be accomplished with a just a hidden field and some conditional statements.

Listing entries from multiple folders

What we’re after here is to apply different formatting to an entry depending on what folder it’s originating from. For example, this site has two types of entries: “articles” and “notes”. On the homepage of the site, I’m pulling in both types of entries using the entries:listing tag in Statamic and declaring the folders I want to pull from folder="articles|notes":

{{ entries:listing folder="articles|notes" }}

  <article>
    <h1>{{ title }}</h1>
    {{ content }}
  </article>

{{ /entries:listing }}

This will work fine, but, what if we want to change the markup for entries pulled from the articles folder? For this example, let’s make articles have a thumbnail while notes will just remain titles and content.

Unfortunately, what won’t work is using a conditional that checks where the content is coming from like so:

{{ entries:listing folder="articles|notes" }}

  {{ if folder="articles"}}

  <article>
    <h1>{{ title }}</h1>
    <img src="{{ thumbnail }}" alt="Thumbnail" />
    {{ content }}
  </article>

  {{ else }}

  <article>
    <h1>{{ title }}</h1>
    <img src="{{ thumbnail }}" alt="Thumbnail" />
    {{ content }}
  </article>

  {{ endif }}

{{ /entries:listing }}

Again, that^ DOESN’T WORK. Perhaps in a future version it will (I’m on 1.8.1 as of writing ), but for now we’ll need to find another way. So here’s how I was able to accomplish this.

Add a hidden field to your entry fieldset

We need some way to identify each type of entry and we can do so by creating a hidden fieldset within the entry’s fieldset yaml file, like so:

fields:
  entry:
    type: hidden
    default: article

Now, when we create a new entry, this field will be inserted in the content file and we can check for it using a conditional statement. Now, we can display images like so:

{{ entries:listing folder="articles|notes" }}

  {{ if entry == "article"}}

  <article>
    <h1>{{ title }}</h1>
    <img src="{{ thumbnail }}" alt="Thumbnail" />
    {{ content }}
  </article>

  {{ else }}

  <article>
    <h1>{{ title }}</h1>
    <img src="{{ thumbnail }}" alt="Thumbnail" />
    {{ content }}
  </article>

  {{ endif }}

{{ /entries:listing }}

That should do it. You can, of course, use this condition to change other markup for each entry. The nice part about all this is that your entries will be listed together, chronologically.

Comments

blog comments powered by Disqus