1

I am migrating a site from Craft 2 - Craft 3. In my sitemap template I have the following code;

{% for section in craft.sections.getAllSections() %}
  {# We only care about sections that have URLs #}
  {% if section.hasUrls %}
    <div class="article mb-8">
      <h2 class="pt-6 mb-4 border-bottom" id="{{  section.name|kebab }}">{{ section.name }}</h2>
      <ul class="ml-4 list-reset">
        {# Loop through each of its entries #}
        {% for entry in craft.entries.sectionId(section.id).limit(null) %}
          <li class="mb-2 text-sm"><a href="{{ entry.url }}" class="arrow--before">{{ entry.title }}</a></li>
        {% endfor %}
      </ul>
    </div>
  {% endif %}
{% endfor %}

I am getting the error Calling unknown method: craft\models\Section::hasUrls()

I cannot find the equivalent or updated value in the Craft 3 docs. Can anyone advise as to how this should be rewritten or what I should use instead of if section.hasUrls in Craft 3?

2

In Craft 3, hasURL() is part of a section's siteSettings() as a section exists in multiple sites and can have URLs in one site but not another.

If you are not using multi-sites, a drop-in replacement for your code would be:

{% for section in craft.sections.getAllSections() %}
  {% for sectionSettings in section.siteSettings %}
      {# We only care about sections that have URLs #}
      {% if sectionSettings.hasUrls %}
        <div class="article mb-8">
          <h2 class="pt-6 mb-4 border-bottom" id="{{  section.name|kebab }}">{{ section.name }}</h2>
          <ul class="ml-4 list-reset">
            {# Loop through each of its entries #}
            {% for entry in craft.entries.sectionId(section.id).limit(null) %}
              <li class="mb-2 text-sm"><a href="{{ entry.url }}" class="arrow--before">{{ entry.title }}</a></li>
            {% endfor %}
          </ul>
        </div>
      {% endif %}
  {% endfor %}
{% endfor %}
|improve this answer|||||

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.