2017-03-08 91 views
0

我有一個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

+0

可以你展示你試過的東西,爲什麼它不起作用? – Kaddath

+0

我試着在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

回答

0

您需要檢查是否$node->getElementsByTagName('enclosure')->item(0)回報給你一些東西。例如:

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' => false, 
     ); 
    $enc_node = $node->getElementsByTagName('enclosure')->item(0); 
    if (!empty($enc_node)) { 
     $itemRSS['artikkelbilde'] = $enc_node->getAttribute('url'); 
    } 

    array_push($itemArr, $itemRSS); 
    $display--; 
} 

稍後,您可以檢查是否artikkelbilde沒有空,你所需要的:

if ($item['artikkelbilde']) { 
    echo 'One thing'; 
} else { 
    echo 'Another thing'; 
} 
+0

但我仍然需要幾行代碼去的地方: 'array_push($ itemArr,$ itemRSS); $ display--; } return $ itemArr;' – skoen

+0

請參閱更新請 –

+0

它的工作。查看我的答案獲取完整的解決方案。謝謝你,u_mulder – skoen

0

我現在這樣做,似乎已經奏效:

<?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' => false 
    ); 
$enc_node = $node->getElementsByTagName('enclosure')->item(0); 
if (!empty($enc_node)) { 
$itemRSS['artikkelbilde'] = $enc_node->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) { 
$sakbilde = $item['artikkelbilde']; 
if ($item['artikkelbilde']) { 
$sakbilde = $item['artikkelbilde']; 
} 
else { 
$sakbilde = 'http://hamarradioen.no/wp-content/uploads/2017/03/hamararbeiderblad900x600.jpg'; } 

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="'. $sakbilde .'" 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>'; }