我有两个单独的 WordPress 安装 - 我将一个称为站点 A,另一个称为站点 B。我想使用 fetch_feed()
将提要从站点 B 拉到站点 A。我还想包括一个缩略图。默认情况下,WordPress 不在提要中包含缩略图,因此我创建了一个自定义提要,其中包括以下内容:
<?php if(get_the_post_thumbnail()): ?>
<media:thumbnail url="<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'feed-thumb'); echo $image[0]; ?>" />
<media:content url="<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'feed-thumb'); echo $image[0]; ?>" medium="image" />
这似乎有效,并且它在每个内部返回如下内容:
<media:thumbnail url="http://www.site-b.com/wp-content/uploads/2013/01/thumbnail.jpg" />
<media:content url="http://www.site-b.com/wp-content/uploads/2013/01/thumbnail.jpg" medium="image" />
现在,我回到站点 A,并尝试使用 fetch_feed()
提取此提要:
<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/feed.php');
// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed('http://www.site-b.com/custom-feed/');
if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 2.
$maxitems = $rss->get_item_quantity(2);
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items(0, $maxitems);
endif;
if ($maxitems == 0) echo 'No items.';
else
// Loop through each feed item.
foreach ( $rss_items as $item ) :
if ($enclosure = $item->get_enclosure())
{
echo '<img src="' . $enclosure->get_thumbnail() . '" class="feed-thumb" />';
}
?>
<p><?php echo esc_html( $item->get_description() ); ?>
<a href="<?php echo esc_url( $item->get_permalink() ); ?>"
title="<?php echo esc_html( $item->get_title() ); ?>">Continue Reading</a></p>
<?php endforeach; ?>
一切正常,除了缩略图。标题、永久链接和描述都正确返回。但是,缩略图不返回 URL。所以,我只剩下:
<img src="" class="feed-thumb" />
如何返回缩略图的 URL?
感谢您阅读到这里。
最佳答案
Aaand,在拔掉我的头发后,这是因为我忘记在我的自定义提要中包含 Media RSS namespace 。我只是将它包含在我的开头 <rss>
标记并且有效:
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss">
关于php - 我怎样才能拉媒体 :thumbnail from an RSS feed with SimplePie and fetch_feed()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14619891/