这是我的RSS提要格式:
<item>
<title></title>
<link></link>
<description></description>
<pubDate></pubDate>
<guid></guid>
<dc:date></dc:date>
</item>我想使用CSS样式播放最后7篇文章,所以我使用以下代码:
<?php
$url = "**THE URL I AM SCRAPING DATA FROM**";
$rss = simplexml_load_file($url);
$i = 0;
if (!empty($rss))
{
$site = $rss
->channel->title;
$sitelink = $rss
->channel->link;
foreach ($rss
->channel->item as $item)
{
$title = $item->title;
$link = $item->link;
$description = $item->description;
$item->description = strip_tags($item->description);
$date = $item->pubDate;
$pubDate = date('d.m.Y', strtotime($date));
if ($i >= 7) break;
?>
<div class="post-item">
<div class="post-item-wrap">
<div class="post-image">
<a href="<?php echo $link;?>">
<img alt="" src="images/news/nra.jpg">
</a>
</div>
<div class="post-item-description">
<span class="post-meta-date"><?php echo $pubDate;?></span>
<h2><a href="<?php echo $link ?>" target="_blank"><?php echo $title;?>
</a></h2>
<p><?php echo implode(' ', array_slice(explode(' ', $description), 0, 30)) . "..";?></p>
<a href="<?php echo $link;?>" class="item-link">learn more <i class="icon-chevron-right"></i></a>
</div>
</div>
</div>
<?php
$i++;
}
}
?>现在我希望这7个帖子中的每一个都有唯一的id。我需要脚本来生成项目->标题和项目->描述,具体取决于项目->链接。(例如,如果我单击第一个xml,它将带我到页面,在那里我可以显示标题和描述,根据我单击的帖子)
提前谢谢。
发布于 2020-03-10 17:03:15
使用此代码获取最后7项
array_slice($rss->channel->item, -7); 发布于 2020-03-10 17:53:23
如果url是"rss.php?feed=1“或"rss.php?feed=2”
$rssSno=$_GET['feed'];
$output = array_slice($rss->channel->item, ($rssSno-1),1);https://stackoverflow.com/questions/60622276
复制相似问题