我试图在Feed中抓取一个img标签,在WordPress中。以下是我使用的代码:
<?php
/* specify the source feed */ $rss = fetch_feed('http://www.anobii.com/rss_shelf?s=01fe251a6c442bbf8a');
/* checks obj. is created */ if (!is_wp_error( $rss ) ) :
/* specify number of items */ $maxitems = $rss->get_item_quantity(5);
/* create an array of items */ $rss_items = $rss->get_items(0, $maxitems);
endif;
?>
<ul>
<?php if ($maxitems == 0) echo '<li>Content not available</li>';
else
// Loop through each feed item and display each item as a hyperlink.
$firstContent = null;
$firstIsSet = false;
foreach ( $rss_items as $item ) : ?>
<?php
if (!$firstIsSet) {
$firstContent = $item->get_content();
$firstIsSet = true;
}
?>
<?php $doc = new DOMDocument();
@$doc->loadHTML($firstContent);
$imgs = $doc->getElementsbyTagName('img');
$res = $imgs->item(0)->getAttribute('src');
$highres = str_replace("type=3", "type=4", $res);
?>
<li>
<a href="<?php echo $item->get_permalink(); ?>" title="<?php echo esc_html( $item->get_title() ); ?>" rel="external"><?php echo esc_html( $item->get_title() ); ?></a>
</li>
<?php endforeach; ?>
</ul>
<img src="<?php print_r($highres); ?>" style="float:right">
get_title()
和get_permalink
正常运作。但是print_r($highres);
什么也没做。
问题可能是rss feed有点坏(请参阅fetch_feed
链接)..?