我有一個rss訂閱源從本地新聞紙中獲取最新的項目。我從一個致命的錯誤開始了這個工作日,因爲他們發佈了一篇沒有封閉圖片的新聞報道。我想要的是添加一個條件標籤來顯示默認圖像,如果沒有圖像封閉。如果在rss feed中找不到圖像的備用圖片
我的代碼是這樣的:
<?php
function read_rss($display=0,$url='') {
$doc = new DOMDocument();
$doc->load($url);
$itemArr = array();
foreach ($doc->getElementsByTagName('item') as $node) {
if ($display == 0) {
break;
}
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'pubdate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'artikkelbilde' => $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')
);
array_push($itemArr, $itemRSS);
$display--;
}
return $itemArr;
}
?>
<div class="prl-span-12 prl-panel clearfix">
<h5 class="prl-block-title default"><a href="http://h-a.no">Lokale nyheter</a></h5>
<div class="prl-grid prl-grid-divider">
<?php
$items = read_rss(3, 'http://www.h-a.no/rss/rssfeed.aspx?subject1=1');
foreach ($items as $item) {
echo '<div class="prl-span-4"><article class="prl-article"><a class="prl-thumbnail" href="' . $item['link'] . '" title="'.$item['title'].'"><span class="prl-overlay"><img src="'. $item['artikkelbilde'] .'" alt="'.$item['title'].'"><span class="prl-overlay-area o-ha"></span></span></a><h6 class="prl-article-title"><a href="' . $item['link'] . '" title="'.$item['title'].'">'.$item['title'].'</a></h6></article>
</div>';
}
我已經使用了一些在這裏發現的條件碼的嘗試,但我仍然得到這個錯誤:
Fatal error: Call to a member function getAttribute() on null in /home/4/h/hamarradioen/www/wp-content/themes/presslayer-hr/inc/nyhetsfeed-hamar.php on line 18
可以你展示你試過的東西,爲什麼它不起作用? – Kaddath
我試着在foreach循環中加入這個 'foreach($ items as $ item){ $ sakbilde = $ item ['artikkelbilde']; if($ sakbilde === null){ $ sakbilde ='http://hamarradioen.no/wp-content/uploads/2017/03/hamararbeiderblad900x600.jpg'; }' 再變變量在IMG SRC說$ sakbilde代替 – skoen