1

I've went through several examples related to my issue, but I quite couldn't find What I'm looking for.

Well, there was one, but with pre-defined array. What I'm looking for is to only output all my sections, with link and title + at the matched section's URL, display all entries belonging under relevant section.

I've found here one snippet of code, which logic sounds about right to me, but it's like there's missing something.

{% set sections = [] %}
{% for section in craft.sections.getAllSections() %}
{% if section.type == 'channel' %}
{% set sections = sections|merge([section]) %}
{% endif %}
{% endfor %}

Any help, or direction would be appreciated and I believe, it would help those, Who are like me at the beginning of this journey.

Thank you

1 Answer 1

2

There is not really a very simple way to do this. One thing to keep in mind is that channels and structures do not have an 'index' page defined by default. Essentially there are no uri's for channels and structures as a whole, only for the entries within them. You need to create index pages yourself, using:

  • Singles (one for each of your index pages), or
  • a Structure (using different EntryTypes for the different levels and/or page types), or
  • Template files (placed directly in your templates folder)

The other problem is that you may have channels that do not represent pages at all, but are just collections of data to be used elsewhere; likewise, sections do not really have a way to define order. Overall not really a good mechanism to generate nav.

There are, however, other ways to do this.

One solution is to use a dedicated Structure specifically for navigation (called 'primaryNav' for example) with a related entries field that links to other entries and a custom url field to link to other pages as needed. You can then loop through that structure to generate your nav.

Another option is to define all of your top level pages (what might normally be considered singles) within a structure using EntryTypes to differentiate the various page types and define the field requirements (i.e. a structure called 'pages' for example). You can then loop through this structure to generate your nav, using the EntryType to determine which template code to display.

Either of these techniques can be used with other channels and or structures as needed to generate lower level pages where there are many of the same type (i.e. news, blog, articles, projects, etc.).

There are a lot of good examples of this on the exchange site. Also have a look at craft's nav tag in the documentation.

2
  • Thank you Douglas for your respond. Yes, you're right, there are several ways to build navigation. The best fit for my need is category group. Here is the link to arecipe, I found useful - craftcookbook.net/recipes/330 Commented Jan 26, 2015 at 20:04
  • Thank you too! This might be an old post, but it opened my mind as to what EntryTypes really are useful for! Commented Feb 2, 2022 at 14:45

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.