开发者

Using IF-ELSE statements to alter elements in a SimplePie feed based on their source

I'm using SimplePie to combine various RSS feeds, and I want to control the output of each individual feed item based on the source for that item. My ultimate goal is to be able to control the content and style of an item based on where its coming from.

I'm using the code below to sort feed items based on their the source link. Then, based on the source, I want to include the appropriate snippet of PHP to dictate which content to pull.

<?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; ?>

Here's the problem I'm encountering: the开发者_开发问答 first "if" statement works fine if an item is TRUE, but if it is FALSE, then it defaults to the final "else" statement, apparently bypassing the "elseif" statements that are in between.

I've tested different items in the first "if" statement, so I know that my source detection code is working, but anything that is placed after an "elseif" is automatically ignored.

I've looked everywhere to figure out what's wrong here, and from what I can tell I'm using the correct formatting for if/else statements. It's likely I'm making a silly parse error or something, as I'm fairly new to PHP. But any help/advice would be much appreciated!

Example snippet that I would include for an item based on the feed source:

<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>

For reference, here is the SimplePie code I'm using at the top of the page:

<?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();

?>


Try something like:

<?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; ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜