【问题标题】:Using IF-ELSE statements to alter elements in a SimplePie feed based on their source使用 IF-ELSE 语句根据来源更改 SimplePie 提要中的元素
【发布时间】: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-&gt;get_feed()-&gt;get_link();
  • @robert:你的语法是正确的。问题出在其他地方,即$item-&gt;get_feed()-&gt;get_link() 永远不等于"http://example.com/FeedURL2""http://example.com/FeedURL3"
  • 谢谢!这帮助我弄清楚了。我的疏忽太愚蠢了……我在其中一个 URL 中遗漏了一个尾随的“/”。结果代码工作正常,我只是使用了错误的 URL 来识别提要项。

标签: php if-statement simplepie


【解决方案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; ?>

【讨论】:

  • 感谢您的帮助!这也很好用,看起来比我尝试的方法更干净。
猜你喜欢
  • 2020-04-05
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 2018-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多