I'm pretty new to Craft, so I'd like to know if there's a best practice when linking to a section, assuming the section has an index.html template for its "homepage."

I'm currently using the following to loop through all Sections and then give me their URL, but in sort of a hacky way. This is one of a couple ways I've found to do this, but my Homepage singlepage entry outputs as http://example.com/__home__ which is super weird.

{% set sections = craft.sections.getAllSections() %}  

{% for section in sections %}
    <li class="nav__link" id="nav__link--{{ section.handle }}">
        <a href="{{ siteUrl }}{{ section.template|split('/')[0] }}">{{ section }}</a>
{% endfor %}


If the best practice is to do something completely different, then feel free to set me straight! Thanks!

1 Answer 1


I've found what is probably a better solution;

I create a Single section for each Channel/Structure and use that as a directory for that channel/structure. I then loop through all sections and see if they're of the single type, then get the first (and only) entry of that section.

{# Get all Section #}
{% set sections = craft.sections.getAllSections() %}

{# Loop through all single sections #}
{% for singleSection in sections if singleSection.type == 'single' %}

    {# Get the first (and only) entry of that section #}
    {% set singleEntry = craft.entries.sectionId(singleSection.id).first %}

    {# Output #}
     <li class="nav__link">
        <a href="{{ singleEntry.url }}" class="section-link">{{ singleSection }}</a>

{% endfor %}

Let me know if you guys have a better way of doing this!

  • This will work fine until you need a single that you otherwise do not want to include in the nav. You might be better served by creating a dedicated 'navigation' section/structure , with an entry field that points to the correct page. That way you can control the order and what is and is not included. Commented Mar 22, 2016 at 19:48
  • I've gotten around this problem with a lightswitch field to include that particular entry in the nav or not. As always ymmv. Commented Mar 22, 2016 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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