利用wordpress自带的feed解析功能获取其他网站的RSS并展示在网页上

这是一段从某个模板里剥离出来的代码,代码如下:

<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed('http://www.yourdomain.com/?feed');			
// Get RSS failed
if ( is_wp_error($rss) ) {
    $error = $rss->get_error_code();
    if($error == 'simplepie-error') {
	//Simplepie Error
	echo "<div class='updated fade'><p>An error has occured with the RSS feed. (<code>". $error ."</code>)</p></div>";
    }
    return;
 } 
?>
<?php
    $maxitems = $rss->get_item_quantity(30); 
		$items = $rss->get_items(0, 30);
?>
<ul class="themes">
	<?php if (empty($items)){ echo '<li>没有更新</li>'; }else{
		foreach ( $items as $item ) : 
			$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $item->get_content(), $matches);
			$first_img = $matches [1] [0];
		?>
		<li class="theme">
			<div class="thumb"><a target="_blank" href="<?php echo esc_url( $item->get_permalink() ); ?>"><img src="<?php echo $first_img?>" width="300"></a></div>
	<h2><a target="_blank" href="<?php echo esc_url( $item->get_permalink() ); ?>" title="<?php echo esc_html( $item->get_title() ); ?>"><?php echo $item->get_title(); ?></a></h2>
		</li>
		<?php endforeach; } ?>
</ul>

代码很好理解,源站缩略图宽度设置为300px,仅显示缩略图和文章标题。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注