Created
February 17, 2023 15:02
-
-
Save mauricerenck/4ceb03b0d7752b0c5efbec6cdb1300bd to your computer and use it in GitHub Desktop.
kirby rss example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Bei mir spielen mehrere Dateien eine Rolle | |
// Die Query für die einzelnen Beitragsquellen habe ich in collections: https://getkirby.com/docs/guide/templates/collections | |
// blog collection: | |
return function ($site) { | |
return page('blog')->index()->listed(); | |
}; | |
// Die Daten trage ich im controller für die sitemap zusammen: | |
// controller/sitemap.php | |
return function ($site, $page) { | |
$articles = kirby()->collection('blogposts'); | |
$books = kirby()->collection('books'); | |
$notes = kirby()->collection('notes'); | |
$newCollection = new Pages([$articles, $books, $notes]); | |
$articleList = $newCollection->sortBy('date', 'desc', 'readDate', 'desc') | |
->filter(function ($child) { | |
return $child->date()->toDate() <= time(); // nur artikel die nicht in der zukunft liegen | |
}) | |
->filter(function ($child) { | |
return $child->translation(kirby()->language()->code())->exists(); // nur mit übersetzung | |
}) | |
return [ | |
'rss' => $articleList, | |
]; | |
}; | |
// dann habe ich ein template für die content represenation der sitemap | |
// Da hole ich mir ein paar extra Daten, wie das Bild für den RSS-Kanal von der About-Page | |
// Und die rss-items, die ich auf 10 reduziere | |
// Ich habe ein paar extra seiten-methoden für categorien z.b. das müsstest du bei dir anpassen | |
// sitemap.rss.php | |
?> | |
<?php $image = page('about')->ogImage() !== false ? page('about')->ogImage()->toFile()->resize(144) : false; ?> | |
<?php $articles = $rss->paginate(10); ?> | |
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"> | |
<channel> | |
<title><?= $page->rsstitle()->text(); ?></title> | |
<link><?= $site->url(); ?></link> | |
<description><?= $page->description()->text(); ?></description> | |
<language><?= kirby()->language()->code(); ?></language> | |
<lastBuildDate><?= $articles->first()->date()->toDate(DATE_RSS); ?></lastBuildDate> | |
<?php if ($image !== false): ?> | |
<image> | |
<url><?= $image->url(); ?></url> | |
<title><?= $page->rsstitle()->text(); ?></title> | |
<link><?= $site->url(); ?></link> | |
<width><?= $image->width(); ?></width> | |
<height><?= $image->height(); ?></height> | |
</image> | |
<?php endif; ?> | |
<atom:link href="<?= $site->url(); ?>/feed.rss" rel="self" type="application/rss+xml"/> | |
<?php foreach ($articles as $article): ?> | |
<?php $hero = ($article->getHeroImage()) ? $article->getHeroImage()->resize(1000) : '';?> | |
<item> | |
<title><?= $article->title()->xml(); ?></title> | |
<link><?= $article->url(); ?></link> | |
<guid><?= $article->url(); ?></guid> | |
<pubDate><?= $article->date()->toDate(DATE_RSS); ?></pubDate> | |
<dc:creator>Maurice Renck</dc:creator> | |
<?php if ($article->singleCategory() !== false && $article->singleCategory() !== null) : ?> | |
<category><?= $article->singleCategory()->title()->xml(); ?></category> | |
<?php endif; ?> | |
<description><![CDATA[<?= $hero . "\n" . $article->intro()->kirbytext() . (($article->gutenberg()->toBlocks()->isNotEmpty()) ? $article->gutenberg()->toBlocks() : $article->text()->kirbytext()); ?>]]></description> | |
</item> | |
<?php endforeach; ?> | |
</channel> | |
</rss> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment