PHP - How do you limit your RSS Feeds to 3 posts

Asked By Annie Rice on 16-Oct-13 08:54 AM

Is there away to limit this code to only show 3 posts.

<?php

$xml=simplexml_load_file("http://bob.blogspot.co.uk/feeds/posts/default?alt=rss");

foreach ($xml->channel->item as $item) {
    $title = (string) $item->title; // Title Post
    $link   = (string) $item->link; // Url Link
    $description = (string) $item->description; //Description Post
echo '<h2><a href="'.$link.'" target="_blank" title="">'.$title.' </a></h2>';
echo '<p>'.$description.'</p><hr><br/>';
 }
 
?>

Robbe Morris replied to Annie Rice on 16-Oct-13 08:54 AM
Based on your code, just create a "count" variable above the foreach and default it to 0.  Inside the foreach, increase the count variable by 1.  I believe PHP uses the "break" clause to break out of a foreach loop.

count = 0;

foreach(stuff)
{
   echo some stuff
   count = count + 1
   if (count == 3) { break; }
}