3

I'm trying to generate a sitemap and wish to output all the entries that have a section type of 'single' but can't get the the correct syntax.

All of the entries need to return 'single' for {entry.section.type}.

{# This query doesn't work but hopefully helps demo the above question #}

{% for entry in craft.entries.section({'type':'single'}).order('id asc') %}
    <url>
        <loc>{{ entry.url }}</loc>
        <lastmod>{{ entry.dateUpdated.atom }}</lastmod>
        <changefreq>weekly</changefreq>
        {% if entry.section.isHomepage() %}
            <priority>1</priority>
        {% else %}
            <priority>0.8</priority>
        {% endif %}
    </url>    
{% endfor %}

Edit:

This query does work but it's a slow bad way of doing it as it gets EVERY entry and filters after the fact:

{% for entry in craft.entries.order('id asc') if entry.section.type == "single" %}
    <url>
        <loc>{{ entry.url }}</loc>    
        <changefreq>weekly</changefreq>
        {% if entry.section.isHomepage() %}
            <priority>1</priority>
        {% else %}
            <priority>0.8</priority>
        {% endif %}
    </url>
{% endfor %}
11

There's no such method available for craft.entries, at least it's not documented. But you can use craft.sections to get an array of section models and filter them with a conditional within a loop:

{# 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 #}
    {{ singleSection }}
    {{ singleSection.isHomepage() ? '(Home)' : '(not Home)'}}
    {{ singleEntry.dateUpdated.atom }}

{% endfor %}
  • {{ singleEntry.dateUpdated.atom }} is returning an error: Impossible to access an attribute ("dateUpdated") on a NULL variable ("") – jnowland Aug 26 '14 at 6:05
  • @jnowland sorry, just fixed a bug with my code. – carlcs Aug 26 '14 at 6:23
  • How would the code be amended to order the singles listed? – Adam Hopkinson May 18 '17 at 12:38
  • @AdamHopkinson you could try with Super Sort plugin – carlcs May 28 '17 at 13:01
1

This has nothing specifically to do with the question, but this post was very helpful in figuring out how to get all entries that are channels. Just wanted to share in case someone else came across this thread. This will return all entries for every channel.

{% set sections = craft.sections.getAllSections() %}
{% for channelSection in sections if channelSection.type == 'channel' %}  

    {% set channelEntry = craft.entries.sectionId(channelSection.id) %}
        {% for entry in channelEntry %}
            <div class="block {{ channelSection.handle }}">
                <div class="block-header">
                    <h4><a href="{{ entry.url }}">{{ entry.title }}</a></h4>
                </div>
                <div class="block-summary">
                    {{ entry.summary }}
                    <p>
                        <small>(In {{ channelSection }})</small>
                    </p>
                </div>
                <div class="block-footer text-right">
                    <i class="fa fa-ellipsis-h"></i>
                </div>
            </div>
        {% endfor %}

{% endfor %}

As you can see I am using:

{{ channelSection.handle }}

To generate something like myChannelHandle which (could) is useful for creating hooks for css/js.

Hope this helps!

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.