WordPress构建RSS Magpie对象函数:fetch_rss

2022/07/11

WordPress函数fetch_rss从URL构建基于RSS的Magpie对象,fetch_rss()函数位于rss.php,但自3.0.0版本起,已不建议使用rss.php,因此,推荐使用fetch_feed()函数。

fetch_rss( string $url )

本文涉及的内容包括:

函数参数

$url

字符串

Feed URL地址

函数返回值

成功返回MagpieRSS,失败返回false

MagpieRSS Object
(
    [parser] => 
    [current_item] => Array
        (
        )

    [items] => Array
        (
            [0] => Array
                (
                    [title] => WordPress快速添加用户函数:wp_create_user
                    [link] => https://www.dianjin123.com/3653.html
                    [dc] => Array
                        (
                            [creator] => 背字根
                        )

                    [pubdate] => Tue, 05 Apr 2022 08:03:20 +0000
                    [category] => 开发WP函数
                    [guid] => https://www.dianjin123.com/?p=3653
                    [description] => WordPress函数wp_create_user用于插入新用户,与wp_insert_user()函数相比较 […]
                    [summary] => WordPress函数wp_create_user用于插入新用户,与wp_insert_user()函数相比较 […]
                )

            ......

        )

    [channel] => Array
        (
            [link] => https://www.dianjin123.com
            [description] => WP教程
            [lastbuilddate] => Tue, 05 Apr 2022 08:04:10 +0000
            [language] => zh-CN
            [sy] => Array
                (
                    [updateperiod] => hourly	
                    [updatefrequency] => 1	
                )

            [generator] => https://wordpress.org/?v=5.9.3
            [tagline] => WP教程
        )

    [textinput] => Array
        (
        )

    [image] => Array
        (
            [url] => https://www.dianjin123.com/wp-content/uploads/2022/04/cropped-favicon-32x32.webp
            [title] => 点金主题网
            [link] => https://www.dianjin123.com
            [width] => 32
            [height] => 32
        )

    [feed_type] => RSS
    [feed_version] => 2.0
    [stack] => Array
        (
        )

    [inchannel] => 
    [initem] => 
    [incontent] => 
    [intextinput] => 
    [inimage] => 
    [current_field] => 
    [current_namespace] => 
    [_CONTENT_CONSTRUCTS] => Array
        (
            [0] => content
            [1] => summary
            [2] => info
            [3] => title
            [4] => tagline
            [5] => copyright
        )

    [last_modified] => Tue, 05 Apr 2022 08:04:27 GMT
    [etag] => 
    [from_cache] => 1
)

函数使用示例

include_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss('https://www.dianjin123.com/feed/');
$rss_items = array_slice($rss->items, 0, 5);
if(empty($rss_items)) {
	echo '没有文章';
} else {
	foreach($rss_items as $item) {
		echo '<li><a href="' . $item['link'] . '">' . $item['title'] . '</a></li>';
	}
}

发表回复

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