获取带有条件的 rss 用于封闭映像,而不是封闭映像


Fetch rss with Conditional for Enclosured image and not Enclosured

我正在做一个项目,这对我来说是新事物。我需要从网站获取rss内容,并显示描述,标题和图像(缩略图)。现在我注意到一些提要将缩略图显示为附件标签,而另一些则没有。现在我有两者的代码,但我需要了解如何创建一个条件,例如:

如果 rss 返回外壳图像 { 做某事 }否则{获取共同的拇指}

以下是抓取图像的代码:

机箱标签图像:

if ($enclosure = $block->get_enclosure())
{
echo "<img src='"" . $enclosure->get_link() . "'">";
}

非外壳:

if ($enclosure = $block->get_enclosure())
{
    echo '<img src="'.$enclosure->get_thumbnail().'" title="'.$block->get_title().'" width="200" height="200">';
}
===================================================================================================================================================================================================================================================================================================

==

PS:如果我们看一下这两个代码,它们几乎相同,区别是get_thumbnail和get_link。

有没有办法创建一个条件来使用正确的代码并始终显示缩略图?

提前感谢大家!

编辑

这是我现在拥有的完整代码:

include_once(ABSPATH . WPINC . '/feed.php');
if(function_exists('fetch_feed')) {
$feed = fetch_feed('http://feeds.bbci.co.uk/news/world/africa/rss.xml'); // this is the external     website's RSS feed URL
if (!is_wp_error($feed)) : $feed->init();
$feed->set_output_encoding('UTF-8'); // this is the encoding parameter, and can be left unchanged in almost every case
$feed->handle_content_type(); // this double-checks the encoding type
$feed->set_cache_duration(21600); // 21,600 seconds is six hours
$feed->handle_content_type();
$limit = $feed->get_item_quantity(18); // fetches the 18 most recent RSS feed stories
$items = $feed->get_items(0, $limit); // this sets the limit and array for parsing the feed
endif;
}
$blocks = array_slice($items, 0, 3); // Items zero through six will be displayed here
foreach ($blocks as $block) {
//echo $block->get_date("m d Y");
echo '<div class="single">';
if ($enclosure = $block->get_enclosure())
{
    echo '<a href="'.$block->get_link().'" target="_blank"><img class="image_post" src="'.$enclosure->get_link().'" title="'.$block->get_title().'" width="150" height="100"></a>';
}
echo '<div class="description">';
echo '<a href="'.$block->get_link().'" target="_blank"><h3>'. $block->get_title() .'</h3></a>';
echo '<p>'.$block->get_description().'</p>';
echo '</div>';
echo '<div class="clear"></div>';
echo '</div>';  
} 

以下是具有 2 个不同图像标签的 XML 片段:

使用缩略图:view-source:http://feeds.bbci.co.uk/news/world/africa/rss.xml

使用外壳:http://feeds.news24.com/articles/news24/SouthAfrica/rss

有没有办法创建一个条件来使用正确的代码并始终显示缩略图?

当然有。你在问题中没有说是什么阻碍了你,所以我必须假设原因,但我可以想象多个。

原因是有两个以上交替的决定吗?

您可以处理源项没有图像或已具有图像的情况:

if ($enclosure = $block->get_enclosure())
{
    echo '<a href="'.$block->get_link().'" target="_blank"><img class="image_post" src="'.$enclosure->get_link().'" title="'.$block->get_title().'" width="150" height="100"></a>';
}

在您当前的方案中,只有一个额外的交替,使其成为三个:如果外壳是缩略图而不是链接:

  1. 图像(无机箱)
  2. 来自链接的图像(带链接的外壳)
  3. 缩略图中的图像(带缩略图的外壳)

然后你不知道如何创建这个决定。这基本上是其他的 - 如果是为了:

if (!$enclosure = $block->get_enclosure())
{
    echo "no enclosure: ", "-/-", "'n";
} elseif ($enclosure->get_link()) {
    echo "enclosure link: ", $enclosure->get_link(), "'n";
} elseif ($enclosure->get_thumbnail()) {
    echo "enclosure thumbnail: ", $enclosure->get_thumbnail(), "'n";
}

这基本上是在此基础上进行输出。但是,如果将图像 URL 分配给变量,则可以稍后决定输出:

$image = NULL;
if (!$enclosure = $block->get_enclosure())
{
    // nothing to do
} elseif ($enclosure->get_link()) {
    $image = $enclosure->get_link();
} elseif ($enclosure->get_thumbnail()) {
    $image = $enclosure->get_thumbnail();
}
if (isset($image)) {
    // display image
}

如果你把这个或多或少复杂的决定转移到它自己的函数中,阅读起来会变得更好:

$image = feed_item_get_image($block);
if (isset($image)) {
    // display image
}

在决策变得更加复杂之前,这非常有效,但这超出了 Stackoverflow 的答案范围。