【发布时间】:2011-08-02 05:40:18
【问题描述】:
我正在使用 SimplePie 来组合各种 RSS 提要,并且我想根据该项目的来源控制每个提要项目的输出。我的最终目标是能够根据项目的来源来控制项目的内容和样式。
我正在使用下面的代码根据源链接对提要项目进行排序。然后,根据来源,我想包含适当的 PHP 的 sn-p 来指示要提取哪些内容。
<?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 还很陌生。但任何帮助/建议将不胜感激!
我将为基于提要源的项目包含的示例 sn-p:
<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();
?>
【问题讨论】:
-
在 else 块中尝试
echo $item->get_feed()->get_link();。 -
@robert:你的语法是正确的。问题出在其他地方,即
$item->get_feed()->get_link()永远不等于"http://example.com/FeedURL2"或"http://example.com/FeedURL3" -
谢谢!这帮助我弄清楚了。我的疏忽太愚蠢了……我在其中一个 URL 中遗漏了一个尾随的“/”。结果代码工作正常,我只是使用了错误的 URL 来识别提要项。
标签: php if-statement simplepie