RSS-Plugin - Coverimage in RSS-Feed?

Hi there!

Iā€™m new to Kirby, and Iā€™m loving it. :heart_eyes:

Today I setup a RSS-Feed, following this recipe. I would like to add the coverimage of a blogpost, but I was not able to do so. Could you please help?

The search is giving no results.

Thank you in advance!

Dennis


What you might want know about newbies:
Iā€™m just a hobbyist. I got some poor skills reading and writing HTML, CSS and just a little PHP. My first homepage used a HTML-Template, then I switched to Wordpress and later to Koken. Since Koken looks abandoned, I want to switch again - and bought a ā€œKirby 3 Pro License + Fellowship Packageā€ for my hobby. :grinning: Now Iā€™m ā€œdevelopingā€ :wink: a fictional travelblog on Kirby2 to get familiar with Kirby. After that, Iā€™ll try to get a new homepage with Kirby3.
Years ago I learned English in school. If you donā€™t use it, youā€™ll loose it. Be warned: My English is sometimes bad.

Hey there, welcome to the forum.

As far as I can tell, images should be part of the description, so if you have added an image to the textarea field you are using for the description with a Kirby Image Tag, it should come out in the feed.

If not, or you want to pull the image from another field or from the images on the page, you will probably need to edit the template for the feed slightly. If you look inside the files for the plugin, you will see this line in template.php

<description><![CDATA[<?php echo $item->{$textfield}()->kirbytext() ?>]]></description>

You should be able to add an image to that, either before or after the echo for the description field. Remember to use PHP to check the image actually exists before trying use it, or it will break the feed if its missing for some reason.

If you want to take it a bit further, you could extend the plugin a little and add an image field to the default options array in feed.php. That way you can pass it through when you set the plugin up in your template, rather than doing a minor hack to the feed template.

Edit: looks like you can use a custom snippet rather then modifying the original template.php. if you copy that into your snippets folder, and tell the plugin you want to use that instead, you should be able to modify away without messing with the original feed code.

1 Like

Hi Dennis,

here is an example with an image

https://forum.getkirby.com/t/rss-feed-with-images/1835/5?u=texnixe

Instead of the foreach loop which goes through all the site files, you can fetch a single image here instead.

The adapted code would then look like this:

Fetch the first image in the folder:

<?php  if($image = $item->images()->first()): ?>
  <figure>
    <a href="<?php echo $image->url() ?>">
    <img src="<?php echo $image->url() ?>">
    </a>
  </figure>
<?php endif ?>

Or, if you want to fetch an image from a coverimage field:

<?php  if($image = $item->coverimage()->toFile()): ?>
  <figure>
    <a href="<?php echo $image->url() ?>">
    <img src="<?php echo $image->url() ?>">
    </a>
  </figure>
<?php endif ?>

Adapt to use your field name.

3 Likes

@texnixe and @jimbobrjames Thank you for your help. But, sorry, I donā€™t get it. :frowning: ā€œNewbie-Alarmā€!

My vanilla-rss-generator looks like this:

<!-- generator="<?php echo $generator ?>" -->
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">

  <channel>
    <title><?php echo xml($title) ?></title>
    <link><?php echo xml($link) ?></link>
    <generator><?php echo c::get('feed.generator', 'Kirby') ?></generator>
    <lastBuildDate><?php echo date('r', $modified) ?></lastBuildDate>
    <atom:link href="<?php echo xml($url) ?>" rel="self" type="application/rss+xml" />

    <?php if(!empty($description)): ?>
    <description><?php echo xml($description) ?></description>
    <?php endif ?>

    <?php foreach($items as $item): ?>
    <item>
      <title><?php echo xml($item->title()) ?></title>
      <link><?php echo xml($item->url()) ?></link>
      <guid><?php echo xml($item->id()) ?></guid>
      <pubDate><?php echo $datefield == 'modified' ? $item->modified('r') : $item->date('r', $datefield) ?></pubDate>
      <description><![CDATA[<?php echo $item->{$textfield}()->kirbytext() ?>]]></description>
    </item>
    <?php endforeach ?>
  </channel>
</rss>

Maybe I misplaced the code. My (non professional) guess is, to put the snipped above of the -tag. That caused trouble, only one blogpost was in that feed.

Interesting things: The RSS-Plugin is showing the all images by default, but not the cover-image. And the code is trying to get a date from that post. But itā€™s not working. There is no ā€œdatefieldā€ I guess, my blogpost is showing a ā€œintro-textā€ with the date.

Is my plugin outdated? Did I something wrong?

My plan (since yesterday) is to get a RSS-Feed with headline, date, coverimage, text and all images. If you can push e in the right direction - it would be niceā€¦ :slight_smile:

I am not sure if I get your request 100%, but hereā€™s how I handle the coverimages in my RSS feeds:

Itā€™s based off Bastianā€™s RSS feed plugin, but with this modification to the template (after the description):

<?php if( $coverImage = $item->coverImage()->toFile() ): ?>
    <enclosure url="<?= $coverImage->url(); ?>" length="<?= $coverImage->size(); ?>" type="<?= $coverImage->mime(); ?>" />
<?php else: // placeholder image ?>
    <enclosure url="http://mydomain.tld/assets/images/article-cover-placeholder.png" length="9055" type="image/png" />
<?php endif; ?>

I also needed a placeholder image (as you can see), but that is not obligatory :wink:

@texnixe @jimbobrjames @bvdputte - Thanks for helping out, but I donā€™t get some things. :frowning: Itā€™s the first time, Iā€™m stuck with Kirby.

First of all, I set my template.php back to the default. When I now take a look at my RSS-Feed, I got eight articles.


RSS-Feed without publishing-date:

The RSS-Feed got no date. Itā€™s not showing, when the article was written. When I take a look at that code (link above), Iā€™ll find:

<lastBuildDate><?php echo date('r', $modified) ?></lastBuildDate>

When Iā€™m opening up an article in my browser and take a look at my source-code there is a date:

<header class="article-header">
        <h1>Das ist nur ein Test</h1>
        <div class="intro text"> Dienstag, 17. Juli 2018</div>
        <hr />
</header>

So, a date is given, but there is now date in that RSS-Feed. :frowning:


Cover-Image:
According to @jimbobrjames Iā€™ve to add code to the description. Okay, now Iā€™ve done that:

 <description>

<?php  if($image = $item->coverimage()->toFile()): ?>
  <figure>
    <a href="<?php echo $image->url() ?>">
    <img src="<?php echo $image->url() ?>">
    </a>
  </figure>
<?php endif ?>


<![CDATA[<?php echo $item->{$textfield}()->kirbytext() ?>]]></description>

The feed is empty. Same, if I add the code in front of the </description>.


All images:
Than Iā€™ve tried @texnixe suggestion. Now I can see all images in my RSS-Feed - great. :slight_smile: Funny thing: When there is a Coverimage and Iā€™m using the same image in my article, only one of them shows up. Is that by accident?

Think of my fictional travel-blog. I got a nice picture of Sagrada FamĆ­lia in Barcelona and Iā€™m using that image as coverimage. Later in the article Iā€™m explaining something for that temple, and will show the image again. How would I do that?


@jimbobrjames : Youā€™re suggesting a ā€œcustom snippetā€. Iā€™m new to Kirby and havenā€™t read about custom snippets. What is the best way for newbies?

See here for everything you need to know about snippets. Basically you can use them to break your code up into reusable snippets. So for example, you can use them for things like the header, footer, menus etcā€¦ anything that gets used on more then one template. It means you do not have to edit the same code in multiple places. It makes it possible to share common bits of code.

In the case of the RSS feed plugin, you can copy the default template into your snippets folder and modify it. There is an option on the plugin to use an alternative instead of the default code for the feed. This way, you can edit without breaking the plugins original functionality.

:slight_smile:

I think your problem is that it is not inside the CDATA statement. Try this:

 <description>
<![CDATA[<?php if($image = $item->coverimage()->toFile()): ?> <figure> <a href="<?php echo $item->url() ?>"> <img src="<?php echo $image->url() ?>"> </a> </figure> <?php endif ?><?php echo $item->{$textfield}()->kirbytext() ?>]]></description>

I also amended the link target, since you probably want that to go to the page the image is on, rather then the image directly.