1

I am trying to retrieve all Channel sections' names/titles. This snippet contains the troublesome part.

{% set allSections = craft.entries.all() %}
    {% for section in allSections %}
        {% if section.getType() == "channel" %}
            {% set navBarLinks = navBarLinks|merge([section.title]) %}
        {% endif %}
    {% endfor %}

allSections contains all entries (I know I am looking for sections but I don't know how to loop all sections. craft.sections returns null, since it's deprecated from Craft 2). ex:

I can see the entry inside a channel 'webshop/overview'. But I want to get only the name of the channel ('webshop' in this case). Also I have 2 other channels without any entries inside them.

navBarLinks currently returns null. I want it to contain the names/titles of the Channel sections regardless if they contain entries inside them.

P.S. I am doing all this so I can have a dynamic navigation bar(similar to breadcrumbs but not exactly). I know Structure will make my life a lot easier but it doesn't quite fit my needs.

1 Answer 1

2

This kind of solved my problem:

{% for section in craft.app.sections.allSections %}
    {% if section.type == 'channel' %}
        {% set navBarLinks = navBarLinks|merge([section]) %}
    {% endif %}
{% endfor %}
2
  • if it solved your problem, you can accept your post as an answer.
    – inrsaurabh
    Commented Aug 26, 2019 at 13:53
  • I can do that in 2 days apparently. Thanks!
    – ves
    Commented Aug 26, 2019 at 13:55

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.