您可以使用包装器fetch_feed()
作为一个简单的短代码。
非常基本的示例;有关更多选项,请参阅RSS小部件代码。
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 Feed Shortcode
* Description: Use <code>[feed url="http://wordpress.stackexchange.com/feeds"]</code> to display a list of feed items.
*/
add_shortcode( \'feed\', \'t5_feed_shortcode\' );
function t5_feed_shortcode( $attrs )
{
$args = shortcode_atts(
array (
\'url\' => \'http://wordpress.stackexchange.com/feeds\'
),
$attrs
);
// a SimplePie instance
$feed = fetch_feed( $args[ \'url\' ] );
if ( is_wp_error( $feed ) )
return \'There was an error\';
if ( ! $feed->get_item_quantity() )
return \'Feed is down.\';
$lis = array();
foreach ( $feed->get_items(0, 20) as $item )
{
if ( \'\' === $title = esc_attr( strip_tags( $item->get_title() ) ) )
$title = __( \'Untitled\' );
$lis[] = sprintf(
\'<a href="%1$s">%2$s</a>\',
esc_url( strip_tags( $item->get_link() ) ),
$title
);
}
return \'<ul class="feed-list"><li>\' . join( \'</li><li>\', $lis ) . \'</ul>\';
}