8

Do you only have to build the .rss template file and then you use that URL for your RSS feed? Based on the documentation at hand, that's what it seems like.

I'm attempting to parse an RSS feed using the Google Feed API (https://developers.google.com/feed/) and it's having issues reading the output - so I just want to see if anyone else out there is using RSS in Craft so I can see their output vs mine. When I view mine, it doesn't look like a traditional xml output.

1
  • I am facing issues to read external RSS feed which is <rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0"> If I create a plain php file with the parsing code, it works fine and returns me the array of feed, but if I put it in plugin controller, it is giving me an empty array. So if anyone has any clues, or reference, please give me. I am trying to achieve it since 4 days now. Commented Oct 29, 2018 at 6:22

3 Answers 3

17

Here's the boilerplate I've been using for RSS feed templates. I just name the file rss.html and call it a day.

This assumes a channel is set up with a description and title field.

{% header "Content-Type: application/rss+xml" %}
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">

{% set rss = craft.entries({ section: 'yourChannel' }) %}

<channel>
  <title>Title Your RSS Feed</title>
  <link>http://www.yourlink.com/</link>
  <description>Enter a description here</description>
  <language>en-us</language>
  <atom:link href="{{ url() }}" rel="self" type="application/rss+xml" />

  {% for item in rss %}
  <item>
    <title>{{ item.title }}</title>
    <link>{{ item.url() }}</link>
    <description>{{ item.description }}</description>
    <pubDate>{{ item.postDate.rfc2822() }}</pubDate>
    <guid>{{ item.url() }}</guid>
  </item>
  {% endfor %}
</channel>

</rss>
3
  • 1
    In other locales than english, date('r') seems to be translated and no longer results in a valid RSS feed — use item.postDate.rfc2822() instead. Commented Mar 12, 2015 at 10:47
  • I have used that plugin but confused how to use it and add field. us-cert.gov/ncas/all.xml this is what i want to extract Commented Mar 30, 2016 at 6:33
  • Make sure to use the escape filter on whatever you're plugging in to the description field: {% set description = description|e %}, because RSS feeds can break with stuff like &nbsp; that appear in rich text field.
    – 4midori
    Commented Oct 17, 2016 at 16:32
3

I just used {% header %} and changed it to application/xml instead of application/rss+xml and got the result I was expecting.

2

I made an article about RSS feed template component for Craft CMS - you can read it here:

http://craftsnippets.com/articles/rss-feed-template-component-for-craft-cms

1
  • Hi there - love your example - I was just wondering if there is anyway to make it so you could add the websites address to a RSS aggregator and it would find the RSS link if its avalible? So far your boilerplate code allows me to reference the RSS to a agrigator but I have to give it the absolute file path i.e. example.com/feed.rss instead of example.com/resources for example
    – Wally
    Commented Apr 19, 2021 at 17:02

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.