0

我正在使用 SimplePie 来组合各种 RSS 提要,并且我想根据该项目的源来控制每个提要项目的输出。我的最终目标是能够根据项目的来源来控制项目的内容和样式。

我正在使用下面的代码根据源链接对提要项目进行排序。然后,根据源代码,我想包含适当的 PHP 片段来指示要提取哪些内容。

<?php foreach ($feed->get_items($start,$length) as $item):

    if ($item->get_feed()->get_link()=="http://example.com/FeedURL1"):
            include 'includes/FeedSource1.html';

    elseif ($item->get_feed()->get_link()=="http://example.com/FeedURL2"):
            include 'includes/FeedSource2.html';

    elseif ($item->get_feed()->get_link()=="http://example.com/FeedURL3"):
            include 'includes/FeedSource3.html';

    else:
            echo '<li>fail</li>';
    endif;

endforeach; ?>

这是我遇到的问题:如果项目为 TRUE,则第一个“if”语句可以正常工作,但如果为 FALSE,则默认为最终的“else”语句,显然绕过了介于两者之间的“elseif”语句.

我在第一个“if”语句中测试了不同的项目,所以我知道我的源检测代码正在工作,但是放在“elseif”之后的任何内容都会被自动忽略。

我到处寻找以找出这里出了什么问题,据我所知,我正在为 if/else 语句使用正确的格式。很可能我正在犯一个愚蠢的解析错误或其他什么,因为我对 PHP 还很陌生。但任何帮助/建议将不胜感激!

我将为基于提要源的项目包含的示例代码段:

<li>
<a href="<?php echo $item->get_permalink(); ?>">
<?php echo substr($item->get_title(), 0, 250) . ''; ?>
<br><span> <?php echo $item->get_date('m.d.y / g:ia'); ?></span></a>
</li>

作为参考,这里是我在页面顶部使用的 SimplePie 代码:

<?php

//get the simplepie library
require_once('simplepie.inc');

//grab the feed
$feed = new SimplePie();

$feed->set_feed_url(array(
    'http://example.com/FeedSource1.rss',
    'http://example.com/FeedSource2.rss',
    'http://example.com/FeedSource3.rss',
));

//enable caching
$feed->enable_cache(true);

//provide the caching folder
$feed->set_cache_location('cache');

//set the amount of seconds you want to cache the feed
$feed->set_cache_duration(600);

//init the process
$feed->init();

//control how many feed items are shown
$start = 0;
$length = 25;

//let simplepie handle the content type (atom, RSS...)
$feed->handle_content_type();

?>
4

1 回答 1

1

尝试类似:

<?php foreach ($feed->get_items($start,$length) as $item):
    $link = $item->get_feed()->get_link();
    switch ($link) {
        case 'http://example.com/FeedURL1':
            include 'includes/FeedSource1.html';
            break;
        case 'http://example.com/FeedURL2':
            include 'includes/FeedSource2.html';
            break;
        case 'http://example.com/FeedURL3':
            include 'includes/FeedSource3.html';
            break;
        default:
            echo 'fail';
            break;
    }
endforeach; ?>
于 2011-08-01T21:40:18.470 回答