Reputation: 71
This may well be a silly question to many, but I've fried my brain sorting out various things today and can't think where to start. Sorry in advance.
I'm pulling in an RSS feed from a Wordpress site to another webpage. That is working and displaying a list of all the posts.
I'm using this code (from another post I made):
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
}
echo "</ul>";
}
getFeed("http://your-wordpress-site/feed/");
I would however like to only show the first 8 entries.
Would I need to use something other than a foreach loop?
Upvotes: 0
Views: 2677
Reputation: 467
You could just keep a counter, then use break
to exit the loop when you hit the maximum value you want.
$i = 0;
foreach($x->channel->item as $entry) {
echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
$i++;
if ($i >= 8){
break;
}
}
Upvotes: 3