3

I've got a google news feed I display in my WordPress site, using the following code:

$feed = fetch_feed($rss_url); // specify the source feed
$limit = $feed->get_item_quantity(20); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
foreach ($items as $item) : 
    echo $item->get_description(); 
endforeach;

Problem is, certain individual articles I need to filter out. Google news items have guid tags. Given the guid of the item, how can I tell SimplePie to ignore the given item?

Thanks-

4

1 回答 1

3

SimplePie 还没有内置过滤功能。但是,您可以选择性地仅显示您想要的项目:

$feed = fetch_feed($rss_url); // specify the source feed
$limit = $feed->get_item_quantity(20); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
$ignoreGUIDs = array("http://example.com/feed?id=1", "http://example.com/feed?id=2");
foreach ($items as $item) : 
    if(!in_array($item->get_id(false), $ignoreGUIDs)){
        echo $item->get_description();
    }
endforeach;

get_id () 方法<guid>返回项目的、<link><title>标签的数组,in_array()然后子句搜索每个标签的匹配项$ignoreGUIDs。如果没有匹配项,则表示该项目的 GUID 不在您的排除列表中,因此显示该项目(由echo)。

于 2011-05-18T21:24:43.303 回答