我正在开发一个基于WordPress&;的网站;Genesis框架。我想显示一个RSS提要From Feedstich 在WordPress页面的内容中间。我的最佳选择是什么?是否有一个可以使用短代码的插件,或者是否有一种方法可以在页面中使用php来显示它们?
在页面上显示外部RSS提要
2 个回复
最合适的回答,由SO网友:fuxia 整理而成
您可以使用包装器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>\';
}
SO网友:sMyles
下面是直接来自WordPress codex的示例代码https://codex.wordpress.org/Function_Reference/fetch_feed:
<h2><?php _e( \'Recent news from Some-Other Blog:\', \'my-text-domain\' ); ?></h2>
<?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://example.com/rss/feed/goes/here\' );
$maxitems = 0;
if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity( 5 );
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items( 0, $maxitems );
endif;
?>
<ul>
<?php if ( $maxitems == 0 ) : ?>
<li><?php _e( \'No items\', \'my-text-domain\' ); ?></li>
<?php else : ?>
<?php // Loop through each feed item and display each item as a hyperlink. ?>
<?php foreach ( $rss_items as $item ) : ?>
<li>
<a href="<?php echo esc_url( $item->get_permalink() ); ?>"
title="<?php printf( __( \'Posted %s\', \'my-text-domain\' ), $item->get_date(\'j F Y | g:i a\') ); ?>">
<?php echo esc_html( $item->get_title() ); ?>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
https://developer.wordpress.org/reference/functions/fetch_feed/
结束