wp_rss()
云策文档标注
概述
wp_rss() 函数用于获取并显示指定 RSS 源的条目,以 HTML 有序列表形式输出。它支持自定义显示条目数量,并包含错误处理机制。
关键要点
- 函数 wp_rss( $url, $num_items = -1 ) 接受两个参数:必需参数 $url 为 RSS 源 URL,可选参数 $num_items 指定显示条目数,默认 -1 表示显示全部。
- 内部使用 fetch_rss() 获取 RSS 数据,通过 array_slice() 限制条目数,并循环输出每个条目的标题、链接和描述,使用 esc_url()、esc_attr() 和 esc_html() 进行安全转义。
- 如果获取 RSS 失败,函数会输出错误消息,提示用户稍后重试。
代码示例
wp_rss( 'http://example.com/rss/feed/goes/here', 5 );
原文内容
Display all RSS items in a HTML ordered list.
Parameters
$urlstringrequired-
URL of feed to display. Will not auto sense feed URL.
$num_itemsintoptional-
Number of items to display, default is all.
Default:
-1
Source
function wp_rss( $url, $num_items = -1 ) {
if ( $rss = fetch_rss( $url ) ) {
echo '<ul>';
if ( $num_items !== -1 ) {
$rss->items = array_slice( $rss->items, 0, $num_items );
}
foreach ( (array) $rss->items as $item ) {
printf(
'<li><a href="%1$s" title="%2$s">%3$s</a></li>',
esc_url( $item['link'] ),
esc_attr( strip_tags( $item['description'] ) ),
esc_html( $item['title'] )
);
}
echo '</ul>';
} else {
_e( 'An error has occurred, which probably means the feed is down. Try again later.' );
}
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 2 content
Drew Jaynes
wp_rss( 'http://example.com/rss/feed/goes/here', 5 );