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