如果在 rss 提要中找不到图像,则回退图像

Fallback image if no image found in rss feed

我有一个 RSS 提要,可以从本地报纸获取最新的项目。我以一个致命错误开始了这个工作日,因为他们发布了一篇没有附上图片的新闻文章。我想要的是添加一个条件标签以在没有包含图像的情况下显示默认图像。

我的代码是这样的:

<?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

您需要检查 $node->getElementsByTagName('enclosure')->item(0) returns 是否适合您。例如:

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';
}

我现在这样做了,它似乎奏效了:

<?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>'; }