bnomei
October 1, 2021, 5:41pm
1
I am creating posts for my plugins to make it easier to find them using the forum search and not just the docs search.
Generate a RSS/JSON/Sitemap-Feed from a Pages-Collection.
site/config/config.php
return [
'routes' => [
[
'pattern' => 'feed',
'method' => 'GET',
'action' => function () {
$options = [
'title' => 'Latest articles',
'description' => 'Read the latest news about our company',
'link' => 'blog'
];
$feed = page('blog')->children()->listed()->flip()->limit(10)->feed($options);
return $feed;
}
],
[
'pattern' => 'sitemap.xml',
'method' => 'GET',
'action' => function () {
$options = [
'images' => false,
'videos' => false,
];
$feed = site()->index()->listed()->limit(50000)->sitemap($options);
return $feed;
}
],
],
];
2 Likes
Peleke
November 9, 2021, 10:01pm
2
Thanks for your plugin!
How can I have two feeds: one for de and one for en on a bilingual website?
bnomei
November 10, 2021, 1:04pm
3
you can define different routes with an explicit language scope.
the page('blog')
will have loaded content from that language then.
in your header where you define the link to the rss you need to make sure it is link the that route. but i guess using site()->url() . '/feed'
should work.
Peleke
December 17, 2021, 3:10pm
4
I tried it with
<a href="<?php echo $kirby->language()->code() . '/rss'; ?>"
and this route but still everything is being shown:
[
'pattern' => 'en/rss',
'method' => 'GET',
'action' => function () {
$options = [
'title' => 'Pelekes Blog',
'description' => 'Die letzten Artikel von Pelekes Blog',
'link' => 'blog'
];
$feed = page('blog')->children()->listed()->filter(function ($child) {
return $child->translation(kirby()->language()->code('en'))->exists();
})->flip()->feed($options);
return $feed;
}
],
Any idea what is missing?
bnomei
December 17, 2021, 8:01pm
5
[
'pattern' => 'rss', // <----------
'language' => 'en', // <----------
'method' => 'GET',
'action' => function ($language) {
$options = [
'title' => 'Pelekes Blog',
'description' => 'Die letzten Artikel von Pelekes Blog',
'link' => 'blog'
];
$feed = page('blog')->children()->listed()->filter(function ($child) {
return $child->translation(kirby()->language()->code('en'))->exists();
})->flip()->feed($options);
return $feed;
}
],
Peleke
December 20, 2021, 9:08am
6
bnomei:
'language' => 'en',
Thanks, that works for the filtering but then it shows only the German content in the feed and not the English version of it. What is missing to show the English content instead?
What I am wondering about is that it shows the german content, while you filter only for english…
Anyway - I am using the same plugin but with these settings:
'language' => '*',
and in the filter:
return $child->translation(kirby()->language()->code())->exists();
And the blog pages are shown in german or english depending on the language choosen by the visitor.
2 Likes
Peleke
December 20, 2021, 3:57pm
8
Thanks for the hint, that did the trick
Now I only have to find out a way to show a translated description for that or just use a short name that doesn’t need to be translated…
texnixe
December 20, 2021, 4:03pm
9
You could set the title and description in the blog page for each language, then fetch these in the correct language in your route, e.g.
'description' => page('blog')->content('en')->get('rssdescription'),
For me, this is working without the ->content('en')
part:
'title' => page('blog')->title(),
'description' => page('blog')->description(),
Peleke
December 21, 2021, 9:16am
11
It works with the title but not with the description (in my case called “text”), which just stays empty while I can access it in a template via <?= $page->text() ?>
:
[
'pattern' => 'rss',
'language' => '*',
'method' => 'GET',
'action' => function () {
$options = [
'title' => page('blog')->title(),
'description' => page('blog')->text(),
'link' => 'blog'
];
$feed = page('blog')->children()->listed()->filter(function ($child) {
return $child->translation(kirby()->language()->code())->exists();
})->flip()->feed($options);
return $feed;
}
],
title: Blog
icon: layers
tabs:
blog:
label: Inhalt
icon: text
columns:
left:
width: 1/4
sections:
meta:
type: fields
fields:
text:
type: text
label: Beschreibung
texnixe
December 21, 2021, 10:47am
12
Peleke:
page('blog')->text(),
The original snippet is very strict and checks if the value is a string, so use
page('blog')->text()->value(),
1 Like
Oziris
June 6, 2022, 12:31pm
14
Hi
I’m using your plugin to make a sitemap but most of my images are stored inside the site
folder, and images are not indexed. On my homepage template, every images are coming from this folder. How to resolve this ?
bnomei
June 8, 2022, 5:25pm
15
the plugin uses the imagesfield
option to find the images.
'imagesfield' => 'images',
<loc><?= $item->{$urlfield}() ?></loc>
<?php foreach (kirby()->languages() as $lang): ?>
<xhtml:link
rel="alternate"
hreflang="<?= $lang->code() ?>"
href="<?= $item->{$urlfield}($lang->code()) ?>"/>
<?php if ($lang->isDefault()): ?><link rel="alternate" href="<?= $item->{$urlfield}() ?>" hreflang="x-default" /><?php endif; ?>
<?php endforeach; ?>
<lastmod><?= date('c', $item->modified()) ?></lastmod>
<?php if ($images): ?>
<?php foreach ($item->{$imagesfield}() as $image): if ($image): ?>
<image:image>
<image:loc><?= $image->url() ?></image:loc>
<?php if ($image->{$imagetitlefield}()->isNotEmpty()): ?><image:title><?= $image->{$imagetitlefield}() ?></image:title><?php endif; ?>
<?php if ($image->{$imagecaptionfield}()->isNotEmpty()): ?><image:caption><?= $image->{$imagecaptionfield}() ?></image:caption><?php endif; ?>
<?php if ($image->{$imagelicensefield}()->isNotEmpty()): ?><image:license><?= $image->{$imagelicensefield}() ?></image:license><?php endif; ?>
</image:image>
<?php endif; endforeach; ?>
<?php endif; ?>
<?php if ($videos): ?>
<?php foreach ($item->{$videosfield}() as $video): if ($video): ?>
you could create a custom page method to let the sitemap know where your image assets are located.
why are you storing the images in site
and not the content
folder?
I’ve been trying to set this up for a while now, but my feed URL is just empty. I’m using a $kirby→collection(…) to get the pages I want in the feed. The collection seems to work, it contains all the pages I want it to contain, but once I add …→feed($options) to the line of code, echo no longer prints anything at all.
<?php
$options = [
'url' => site()->url(),
'feedurl' => site()->url() . '/feed/',
'title' => 'Beachball.tech Feed',
'description' => '',
'link' => site()->url(),
'urlfield' => 'url',
'titlefield' => 'title',
'datefield' => 'date',
'textfield' => 'text',
'modified' => time(),
'snippet' => 'feed/json', // 'feed/json'
'dateformat' => 'r',
'mime' => null,
'sort' => true,
];
$blogposts = $kirby->collection('blogposts');
echo $blogposts->feed($options);
?>
The whole thing is on GH, in case anybody really wants to dig in: Trying to get a feed working · stairjoke/beachball-tech@3bcf385 · GitHub
Any suggestions what I’m doing wrong?
Is it possible to include this xsl file in the RSS feed without hacking the plugin source?
aboutfeeds/pretty-feed-v3.xsl at main · genmon/aboutfeeds (github.com)
Here’s an example of it in action:
Adactio: Journal Web Feed
bnomei
February 7, 2023, 10:04am
18
i would guess adding route with matching name echoing your custom xsl should do. the plugin does not itself register a route for the xls so that should be fine.
[
'pattern' => 'sitemap.xsl',
'method' => 'GET',
'action' => function () {
// content of
// https://github.com/genmon/aboutfeeds/blob/main/tools/pretty-feed-v3.xsl
// in a snippet/aboutfeedxsl.php file
snippet('aboutfeedsxsl');
die;
}
],