1

I'm trying to create RSS feed for a site news section, most of it is straight forward to do but I run into an issue with the description section. That content is a sub field in a matrix field along with other subfields like downloads and block quotes.

I have looked extensively, and I haven't found any examples or ideas on how to do this. I can output the matrix field itself in the rss and it gives me a list of all the fieldnames it contains but I can't find a way to output those sub fields into the RSS feed as items. Thanks for any help

1 Answer 1

1

You can do this by filtering the Matrix field down to just the field block(s) you want using their handle.

{% for entry in craft.entries.section('articles').all() %}
    <item>
        <title>{{ entry.title }}</title>
        <link>{{ entry.url }}</link>
        <pubDate>{{ entry.postDate|rss }}</pubDate>
        <author>{{ entry.author }}</author>
        <guid>{{ entry.url }}</guid>
        <description><![CDATA[
        {% for block in entry.myMatrixField.type('blockTypeHandle').all() %}
             {{ block.textContent|striptags }}
        {% endfor %}
        ]]></description>
    </item>
{% endfor %}

This example filters a Matrix field down to just the blocks that have a handle of blockTypeHandle within the field, and outputs their content, which in this case is a rich text field with a handle of textContent.

3
  • Hi Martin , your code makes sense but it doesn't work for me . My matrix field is called innerContent , my block is called innerText and my field is called text . Here is the code I used <description><![CDATA[ {% for block in entry.innerContent.type('innerText').all() %} {{ block.text|striptags }} {% endfor %} ]]></description>. I get a Twig error Variable "entry" does not exist. I have wrapped the code in with the right craft entries tag as the rss outputs correctly if I leave out that matrix block.
    – bhackett
    Commented Aug 30, 2023 at 13:37
  • Hmm, that's odd. Is there an entry variable available within the {% for .. in %} loop? What do you get if you add a {% dd entry %} statement inside the loop? Commented Aug 31, 2023 at 9:31
  • 1
    Thanks Martin, got it working. I was using item instead of entry to define the Rss fields. Thanks for your help
    – bhackett
    Commented Aug 31, 2023 at 10:35

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.