Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<image> data #60

Open
parrisgjerde opened this issue Feb 3, 2021 · 5 comments
Open

<image> data #60

parrisgjerde opened this issue Feb 3, 2021 · 5 comments

Comments

@parrisgjerde
Copy link

Is there a way to modify feed.rss.twig to include a feature image for the RSS feed?

@rhukster
Copy link
Member

rhukster commented Feb 3, 2021

You can certainly override the twig template in your theme to add any functionality you want. However please note you should test it to make sure it's still a valid feed or your users might not be able to consume it.

@parrisgjerde
Copy link
Author

Thanks

@karless1
Copy link

Ciao, did you manage to include images in RSS? a colleague created a plugin for WP to just add the image, https://github.com/lesion/add-img-to-feed - still looking for a viable solution for grav. We manage to display images into grav feeds using PHP8 + TwigFeed beta https://github.com/OleVik/grav-plugin-twigfeeds/releases/tag/v5.0.0-beta.2 but the necessary twig is far from good - using item.content instead of item.medias - any idea will be appreciated, tnx.

@rhukster
Copy link
Member

rhukster commented Aug 30, 2022

If you are asking about an img that is highlighted for each post, that's already being done via the feed.rss.twig file:

this gets the first image from the page the entry is based on: https://github.com/getgrav/grav-plugin-feed/blob/develop/templates/feed.rss.twig#L20

and this displays it if it's available: https://github.com/getgrav/grav-plugin-feed/blob/develop/templates/feed.rss.twig#L28-L30

If you wanted to provide another image, or provide a URL you could simply add some custom header to the page. Depending on if you want to specify an image in the page folder, or an image in the site 'somewhere' or even an external image, you could override the feed.rss.twig file in your theme and replace the {%set banner... variable bit with something like:

Assuming you specify an external in your page header like this:

feed:
  image: https://source.unsplash.com/gySMaocSdqs/1200x800

or a local image via stream like this:

feed:
  image: user://images/foo.jpg

NOTE: This resolves to user/images/foo.jpg in your filesystem

and then copy and modify the feed.rss.twig file in your theme like this:

{% set banner_file = item.header.feed.image %}
{% banner_file %}
  {% if banner_file starts with 'http' %}
    {# assume external file and just pull the img in as-is #}
    {% set banner = '<img src="' ~ url(banner_file) ~ '" alt="featured img" />' %}
  {% else %}
    {# assume a local file via a stream and we'll use media to load image to allow resizing #}
    {% set banner = media[banner_file].cropZoom(1200,800).html|absolute_url %}
  {% endif %}
{% else %}
  {# fallback is to grab first page image and resize it #}
  {% set banner = cropZoom(1200,800).html|absolute_url %}
{% endif %}

Then we just need to update the bit where it outputs a little as we now resize the image in the variable definition above:

<![CDATA[
{% if banner %}
 {{ banner|raw }}
{% endif %}

Something like that. Not tested, but you get the idea.

@karless1
Copy link

karless1 commented Sep 1, 2022

ciao, many thanks for your contribution! great! I was tinkering with, but you make it far more easier.
I'm testing what you wrote.

Still,

the grav rss template is good for us, but, for our purposes it lacks an enclosure for the image or even better for a media:image tag because right now, AFAIK, it includes the image into the content and it's not trivial to pick it up - somethin like a safe_truncate_html from content as in

{{ item.content|safe_truncate_html(collection.params.length)|raw }}

this was the main reason leading us to re-code another WP plugin, add just a media:image
https://github.com/lesion/add-img-to-feed/blob/nomaster/add-img-to-feed.php#L47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants