I'm relatively new to Twig and PHP in general, so please don't shoot me just yet for asking stupid questions!

Basically, I've got several feeds from RSS to Facebook and Twitter. And I need to merge all of these into a custom array so I can make sure all fields match. I'm gonna output all these later, after sorting them by their published date.

But how do I get started with this thing? Create an empty array first, and then loop through the feeds and push each post into the array? How do I name the fields? This is what I have so far:

{% set feeds_array = [] %}

{% set feed = craft.feedme.feed(my_rssfeed) %}

{% for node in feed %}
    {% set feeds_array = {'title': node.title} %}
{% endfor %}

{% for post in feeds_array %}
{% endfor %}

Is this the way to go about this?


It looks a bit awkward, but to append items to an array you have to use Twig's merge filter, merging the feeds_array with itself for each new item. Here's how I'd do it if you need different logic for each feed:

{% set feeds_array = [] %}

{% for node in craft.feedme.feed(my_rssfeed) %}
    {% set feeds_array = feeds_array|merge([{ title: node.title }]) %}
{% endfor %}

{% for node in craft.feedme.feed(my_otherfeed) %}
    {% set feeds_array = feeds_array|merge([{ title: node.whateverField }]) %}
{% endfor %}

{% for post in feeds_array %}
    {{ post }}
{% endfor %}
| improve this answer | |
  • Great! That looks good :) I've managed to get it working somewhat. But I haven't gotten to the other feed yet, will use the merge filter when I do! Thanx! – Kenny Bones Oct 21 '15 at 13:15

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.