I've been learning Craft 3 for a couple weeks and I've run into a little snag. I have a homepage that I would like to display multiple entry loops and/or types. However, I cannot find how to do this on a Single (section). Ive been experimenting around, and I can get the desired effect by making the homepage a Structure (section) via multiple entry types, but that gets janky with the URL/URi setup (I've seen other articles saying to use the __home__ method, but seems like it gets messy to try and work around that with sub-pages).

Here is kinda what I'm going after for the homepage:

Hero Element (has a few of it's own fields)

Intro Element (has a few of it's own fields)

Image Slider Element (loops through some image asset entries)

MAIN ENTRY LOOP (loops through some entries that you click to go to the full entry (which has its own template))

So in summary, How can I have multiple entry loops/types on a Single, OR Use a Structure section as the homepage without getting to janky with the URL/URi? OR is there a better way to set this up that I'm not thinking of?


You can do this in a Single without too much trouble. Your homepage will presumably have its own template, so the simplest solution is to content-manage the fields for your Hero, Intro and Image Slider in the homepage Single, and then in the homepage template, hardcode the channels which will form the source of your main entry loop. That way, your homepage Single is responsible for managing content that is specific to that page only, and then outputs other content from elsewhere below.

Here's a quick example of how that might look in your homepage template Twig file:

{% set myHeroField = entry.myHeroField %}
{% set myIntroField = entry.myHeroField %}
{% set sliderImages = entry.sliderImagesField %}

<h2>{{ myHeroField.heading</h2>
<p>{{ myHeroField.strapline</p>
<a href="{{ myHeroField.ctaLink.one().url">Read more</a>

<p class="intro">{{ myIntroField.textContent }}</p>
<a href="{{ myIntroField.ctaLink.one().url }}">See more</a>

<div class="carousel">
{% for sliderImage in sliderImages.all() %}
    <img src="{{ slideImage.one().url }}>
{% endfor %}

{% set myContentEntries = craft.entries.section('myContentSection').all() %}

{% for contentEntry in myContentEntries %}
    <!-- Output blog posts etc. -->
{% endfor %}

If you want to get more advanced and flexible, you could use a dropdown or similar in your homepage fields to define which Sections you want to be included in the main entry loop. Sadly, there isn't a first-party Section fieldtype, though there is a third-party plugin (now deprecated, unfortunately). Alternatively, depending on how complex and changeable your content model is, you could use a Dropdown fieldtype and just hardcode your section handles into the values.

In your homepage template, you'd then do something like this to fetch entries to show in the main entry content:

{% set myContentEntries = craft.entries.section(entry.sectionsToShow).all() %} 
  • Only thing I'd change about this answer is you probably want to limit how many entries you want to retrieve (and thus show). Sounds trivial now but can help squash performance issues later. – RitterKnight May 11 '20 at 18:43

Martin Spain's answer covers a tactical way to handle this, but I thought I'd tackle this a different way.

When you use the pre-built routing for entries or categories, Craft does some things behind the scenes to make things easier; for example, it sets the entry or category template variable for you.

However, that can make it seem like you can only grab fields/content from the current entry.

One of the nice things about Craft is the templates you use to display content can really be "decoupled" from the routing. Any template can more or less bring in any piece of content from anywhere else in the system, you just need to know where to find/query it.

You do this with Element Queries.

However, this can cause some confusion if you're used to other systems. (The WordPress loop is a good/terrible example of this—to work around it, you can use WP_Query but it's not as clean and neat.)

The only differences between a Structure, Channel, and Single are the way the UI is presented to the author, the way each piece of content gets routed, and how many pieces of content each section can hold (or to put it another way, how they're grouped together). The actual content is up to you for each section.

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.