工作方式
先用PHP作为接口将Feed转换为Json,再由Js输出为HTML,自定义字段输出在前段Js请求接口的参数中,这样就可以通过自定义字段控制订阅那些RSS。
接口部分PHP
<?php
$feedParam = isset($_GET['feed'])? explode(',', $_GET['feed']) : [];
$rssSources = [];
if (!empty($feedParam)) {
$rssSources = array_map(function ($feed) {
return $feed. 'feed/';
}, $feedParam);
}
if (!empty($rssSources)) {
$allItems = array_reduce($rssSources, function ($carry, $source) {
$rss = simplexml_load_file($source);
foreach ($rss->channel->item as $item) {
$carry[] = [
'title' => (string)$item->title,
'description' => (string)$item->description,
'link' => (string)$item->link,
'pubDate' => (string)$item->pubDate,
'creator' => (string)$item->children('http://purl.org/dc/elements/1.1/')->creator,
];
}
return $carry;
}, []);
usort($allItems, fn($a, $b) => strtotime($b['pubDate']) - strtotime($a['pubDate']));
header('Content-Type: application/json');
echo json_encode(['items' => $allItems]);
} else {
header('Content-Type: application/json');
echo json_encode(['description' => 'No valid feed sources provided']);
}
?>
主题自定义页面部分
<?php
/**
* Feed
*
* @package custom
*/
$this->need('components/header.php');
?>
<div class="PAP">
<div class="PAP-content">
<div id="FriendsArt"><center>正在加载中…<br>若长时间无响应,请尝试刷新页面</center></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const container = document.getElementById('FriendsArt');
fetch('https://i.eastblues.cn/feed.php?feed=<?php echo $this->fields->feedURL;?>')//建议自建API,否则可能会导致我的服务器压力骤增
.then(response => response.json())
.then(data => {
container.innerHTML = '';
data.items.forEach(({ title, creator, pubDate, description, link }) => {
const entryDiv = document.createElement('div');
entryDiv.innerHTML = `
<h3>${title}</h3>
<blockquote><small>作者:${creator}<br>发布日期:${pubDate}</small></blockquote>
<p><q>${description}</q> <a href="${link}" target="_blank">查看详情</a></p><hr>
`;
container.appendChild(entryDiv);
});
});
});
</script>
</div>
</div>
<?php if ($this->fields->enableComment == 1): ?>
<?php $this->need('components/comments.php'); ?>
<?php endif; ?>
<?php $this->need('components/footer.php'); ?>
自定义字段
格式:https://i.eastblues.cn/feed/,地址2,地址3