问题
I'm using Simplepie to parse different RSS feeds, passing them to a Smarty template, and I need to return the attribute from each item line that in this example reads: NEWSX
<source url="http://whatever.url/"><![CDATA[NEWSX]]></source>
I have found the get_item_tags method will select the line and attribute having used the following:
$newssource = $item->get_item_tags('','source');
Here is my problem. I don't know how to attach each source to an item when using the following code (so that basically I can display the different source element each time alongside the usual title, link, description and so on):
$RSS = array();
foreach($items as $item){
$feed = $item->get_feed();
$tmp=array();
$newssource = $item->get_item_tags('','source');
echo $newssource[0]["data"];
if ($feed){
if ($enclosure = $item->get_enclosure()){
$tmp['title'] = $item->get_title();
$tmp['permalink'] = $item->get_permalink();
$tmp['thumbnail'] = $enclosure->get_thumbnail();
$tmp['description'] = $enclosure->get_description();
$tmp['image'] = $enclosure->get_link();
}
$tmp['date'] = $item->get_date('j M Y');
$tmp['content'] = $item->get_content();
$tmp['title'] = $item->get_title();
$tmp['link'] = $item->get_link();
$tmp['description'] = $item->get_description();
array_push($RSS, $tmp);
}
}
Can it be done? Thanks in advance for any help or advice.
回答1:
So this is the solution:
$RSS = array();
foreach($items as $item){
$feed = $item->get_feed();
$tmp=array();
if ($feed){
$tmp['date'] = $item->get_date('j M Y, g:i a');
$tmp['content'] = $item->get_content();
$tmp['title'] = $item->get_title();
$tmp['link'] = $item->get_link();
$tmp['description'] = $item->get_description();
$tmp['source'] = $item->get_item_tags('','source')[0]["data"];
array_push($RSS, $tmp);
}
}
$smarty->assign( $params['assign'], $RSS );
And in the smarty template:
<div class="cont">
<a href="{$entry.link}" target="_new">{$entry.title}</a>
<br />
<span class="date">Published on: <strong>{$entry.date}</strong></span><br />
<span class="source">Via : <strong>{$entry.source}</strong></span><br />
</div>
来源:https://stackoverflow.com/questions/39574448/simplepie-and-get-item-tags-attribute