kevq
October 9, 2023, 8:46am
1
Hey folks,
In my RSS feed, I have the following that populates the contents of the feed:
<![CDATA[<?= $item->text()->kt() ?>]]>
This works absolutely fine for the majority of my standard posts, however, I also have a couple of other templates that I use for different types of posts. These are things like book reviews, or links out to other blogs.
Problem is, when I publish one of these “special” posts, the actual book details or link details aren’t parsed into the RSS feed. Now I understand what that’s happening, it’s because I only explicitly call the KirbyText field in my RSS feed, but I’m unsure how I can call the other elements from my “special” posts?
I’ve included my book
template below to give you an idea of what it contains:
<div class="book">
<?php if($image = $page->cover()->toFile()): ?>
<img class="floatleft" width="185px" src="<?= $image->url() ?>" alt="<?= $image->alt() ?>">
<?php endif ?>
<?php if ($info = $page->info()->toObject()): ?>
<p>
✍️ <b>Written by:</b> <?= $info->author() ?><br>
🏷 <b>Genre:</b> <?= $info->genre() ?><br>
🗓 <b>Published:</b> <?= $info->published() ?><br>
📄 <b>Pages:</b> <?= $info->pages() ?><br>
🧐 <b>My rating:</b> <?= $info->rating() ?>
</p>
<?php endif ?>
<div class="spacer"></div>
<p markdown="1"><?= $page->summary() ?></p>
</div>
<?php if($page->amazon()->isNotEmpty()): ?> <a class="button" href="<?= $page->amazon() ?>">Buy on Amazon</a> <?php endif ?> <?php if($page->kobo()->isNotEmpty()): ?> <a class="button" href="<?= $page->kobo() ?>">Buy on Kobo</a> <?php endif ?>
<?= $page->text()->kirbytext() ?>
Like I said, anything within the kirbytext
fiels is rendered, but all the book meta data etc. isn’t.
Any help would be greatly appreciated.
Thanks,
Kev
kevq
October 9, 2023, 8:55am
2
I suppose logically, the RSS feed would need to say “IF the template is “book” or “link” then render the following fields ELSE render kirbytext”. I just don’t know how to do that.
Which plugin are you using?
kevq
October 9, 2023, 12:38pm
4
I’m not using a plugin for it. I made it myself. Here’s my rss.php
snippet and the appropriate section from my config:
<?php
echo '<?xml version="1.0" encoding="utf-8"?>';
?><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><?= $title ?></title>
<link><?= site()->url() ?></link>
<atom:link href="<?= site()->url() ?>/feed" rel="self" type="application/rss+xml" />
<language>en-GB</language>
<lastBuildDate><?= $posts->first()->published()->toDate('r') ?></lastBuildDate>
<description><?= $description ?></description>
<image>
<url><?= site()->url() ?>/assets/images/favicon.png</url>
<title><?= $title ?></title>
<link><?= site()->url() ?></link>
</image>
<?php foreach ($posts as $item): ?>
<item>
<title><?= Xml::encode($item->title()) ?></title>
<link><?= Xml::encode($item->cleanurl()) ?></link>
<guid isPermaLink="false"><?= Xml::encode($item->id()) ?></guid>
<pubDate><?= $item->published()->toDate('r') ?></pubDate>
<description>
<![CDATA[<?= $item->text()->kt() ?>]]>
<![CDATA[<p style="font-size:1.3rem;" class="feed-email-link"><a href="mailto:<?= Xml::encode(site()->email()) ?>?subject=<?= Xml::encode($item->title()) ?>">Reply to this post by email</a></p>]]>
</description>
</item>
<?php endforeach; ?>
</channel>
</rss>
// Setup the rss feed
[
'pattern' => ['/feed'],
'action' => function () {
$title = "Kev Quirk";
$description = "Latest posts from my blog";
$posts = kirby()->collection('posts')->limit(20);
return new Response(snippet('rss', compact('title', 'description', 'posts') , true), 'application/rss+xml');
}
]
$item here is a page object, so you can decide here what you want to render instead of $item->text()->kt()
So
if (in_array($item->template()->name(), ['book', 'link'])) {
// do stuff
} else {
// do some other stuff
}
kevq
October 9, 2023, 12:42pm
6
Thanks for the reply. I know where to put it in the snippet, I’m just not sure how to build the if statement (if that’s actually what I need) that says “IF the template is “book” or “link” then render the following fields ELSE render kirbytext”.
kevq
October 9, 2023, 1:16pm
7
I’ve tried muddling somethign together, but it isn’t working, the unique piece from the page is still ignored in the RSS feed:
<?php if($item->is(page('link')) == true) : ?>
<![CDATA[
<div class="link">
<h1><?= $page->title() ?></h1>
<span>by <?= $page->author() ?></span>
<p markdown="1"><?= $page->summary() ?></p>
<p><a class="dark-button" target="blank" href="<?= $page->link() ?>">Read Post →</a></p>
</div>
]]>
<?php endif ?>
Is there some way to tell Kirby to pull the entire rendered page (IE everything between the article
tags? That would make things a lot easier.
I posted example code above, did you not see it?
kevq
October 9, 2023, 1:23pm
9
Oops sorry, I missed it. I’ll take a look and will revert back, thanks!
kevq
October 9, 2023, 1:49pm
10
That worked perfectly, thanks @texnixe