PHP rss feed |我只想要一次显示四个公告

时间:2016-05-06 07:24:45

标签: php rss

以下是我目前的代码:

<?php 
                        $feed_url = '<link removed>'; 
                        $xml_data = simplexml_load_file($feed_url);

                        $i=0; 
                        foreach($xml_data->channel->item as $ritem) { 

                        $e_title       = (string)$ritem->title; 
                        $e_link        = (string)$ritem->link; 
                        $e_pubDate     = (string)$ritem->pubDate; 
                        $e_author      = (string)$ritem->author;
                        $e_content     = $ritem->children("content", true);
                        $e_encoded     = (string)$e_content->encoded; 
                        $description   = substr($e_encoded, 0, 350).'<a style="font-weight: bold;" href="' . $e_link . '"> ...Read More</a>';

                        $n = ($i+1);
                        print "\n"; 
                        print '<h5 class="font-accident-one-bold hovercolor uppercase" style="margin-bottom: 5px;"><a href="' . $e_link . '">'. $e_title .'</a></h2>'."\n"; 
                        print '<p class="date" style="margin: 0px;">'. str_replace('+0000', '', $e_pubDate) .'</p>'."\n"; 
                        print '<p style="margin: 0px;"> <strong>By '. str_replace('invalid@example.com', '', $e_author) .'</strong></p>'."\n"; 
                        print '<p class="small" style="margin-top: 5px; padding-bottom: 5px;">'. $description .'</p>'."\n"; 

                        $i++; 
                        }

我正在从我的xenforo论坛中提取RSS源。我想一次只显示四个帖子,因为如果有几个帖子可见,我的页面会变得太多了。我希望它只显示最新的四个帖子。

1 个答案:

答案 0 :(得分:0)

避免休息

$counter = 4;

for(x=0; x= $counter; x++)
{

//do something
//make sure you dont pull same data four times. But try to fetch data one by one. So this loop works for you as this loop makes four rounds
}

或使用继续。

比较你有这样的数据:

 if($product_category>4) continue;

它将跳过循环的控制并继续