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

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.