函数文档

wp_widget_rss_output()

💡 云策文档标注

概述

wp_widget_rss_output() 函数用于在列表中显示 RSS 条目,是 WordPress 中处理 RSS 输出的核心函数。它接受 RSS 源参数和配置参数,并生成格式化的 HTML 列表。

关键要点

  • 函数接受 $rss 参数,可以是字符串(RSS URL)、数组(包含 'url' 键)或对象(SimplePie 对象),并自动调用 fetch_feed() 获取数据。
  • 通过 $args 参数控制输出选项,包括是否显示作者、日期、摘要,以及条目数量,默认值通过 wp_parse_args() 合并。
  • 函数包含错误处理,如 RSS 源无效时显示错误信息(仅对管理员或具有 manage_options 权限的用户)。
  • 输出为无序列表(
      ),每个条目包含链接、标题、可选摘要、日期和作者信息,使用 esc_html()、esc_url() 等函数进行安全转义。
    • 函数在输出后调用 __destruct() 清理资源,避免内存泄漏。

    代码示例

    wp_widget_rss_output( array(
        'url'           => 'http://www.youdomain.com/feed',
        'title'         => __( 'Title Goes Here' ),
        'items'         => 5,
        'show_summary'  => 1,
        'show_author'   => 0,
        'show_date'     => 0
    ) );

    注意事项

    • 确保 RSS 源有效,否则函数可能返回错误或空输出。
    • 在非管理员上下文中,错误信息可能被隐藏,需通过日志或调试工具检查问题。
    • 函数依赖于 WordPress 的 feed 和格式化函数,如 fetch_feed() 和 wp_trim_words(),确保这些函数可用。

📄 原文内容

Displays the RSS entries in a list.

Parameters

$rssstring|array|objectrequired
RSS url.
$argsarrayoptional
Widget arguments.

Default:array()

Source

function wp_widget_rss_output( $rss, $args = array() ) {
	if ( is_string( $rss ) ) {
		$rss = fetch_feed( $rss );
	} elseif ( is_array( $rss ) && isset( $rss['url'] ) ) {
		$args = $rss;
		$rss  = fetch_feed( $rss['url'] );
	} elseif ( ! is_object( $rss ) ) {
		return;
	}

	if ( is_wp_error( $rss ) ) {
		if ( is_admin() || current_user_can( 'manage_options' ) ) {
			echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $rss->get_error_message() ) . '</p>';
		}
		return;
	}

	$default_args = array(
		'show_author'  => 0,
		'show_date'    => 0,
		'show_summary' => 0,
		'items'        => 0,
	);
	$args         = wp_parse_args( $args, $default_args );

	$items = (int) $args['items'];
	if ( $items < 1 || 20 < $items ) {
		$items = 10;
	}
	$show_summary = (int) $args['show_summary'];
	$show_author  = (int) $args['show_author'];
	$show_date    = (int) $args['show_date'];

	if ( ! $rss->get_item_quantity() ) {
		echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>';
		$rss->__destruct();
		unset( $rss );
		return;
	}

	echo '<ul>';
	foreach ( $rss->get_items( 0, $items ) as $item ) {
		$link = $item->get_link();
		while ( ! empty( $link ) && stristr( $link, 'http' ) !== $link ) {
			$link = substr( $link, 1 );
		}
		$link = esc_url( strip_tags( $link ) );

		$title = esc_html( trim( strip_tags( $item->get_title() ) ) );
		if ( empty( $title ) ) {
			$title = __( 'Untitled' );
		}

		$desc = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
		$desc = esc_attr( wp_trim_words( $desc, 55, ' […]' ) );

		$summary = '';
		if ( $show_summary ) {
			$summary = $desc;

			// Change existing [...] to […].
			if ( str_ends_with( $summary, '[...]' ) ) {
				$summary = substr( $summary, 0, -5 ) . '[…]';
			}

			$summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>';
		}

		$date = '';
		if ( $show_date ) {
			$date = $item->get_date( 'U' );

			if ( $date ) {
				$date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
			}
		}

		$author = '';
		if ( $show_author ) {
			$author = $item->get_author();
			if ( is_object( $author ) ) {
				$author = $author->get_name();
				$author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
			}
		}

		if ( '' === $link ) {
			echo "<li>$title{$date}{$summary}{$author}</li>";
		} elseif ( $show_summary ) {
			echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>";
		} else {
			echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>";
		}
	}
	echo '</ul>';
	$rss->__destruct();
	unset( $rss );
}

Changelog

Version Description
2.5.0 Introduced.

User Contributed Notes