WordPress构建RSS Magpie对象函数:fetch_rss

本站正在使用的主机

本站目前托管于腾讯云香港轻量服务器,价格便宜访问速度也快,使用本站推荐链接购买还可免费获得WordPress运行环境优化服务。

最新活动 »

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.beizigen.com/3753.html
                    [dc] => Array
                        (
                            [creator] => 背字根
                        )

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

            ......

        )

    [channel] => Array
        (
            [link] => https://www.beizigen.com
            [description] => 开发运营笔记
            [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] => 开发运营笔记
        )

    [textinput] => Array
        (
        )

    [image] => Array
        (
            [url] => https://www.beizigen.com/wp-content/uploads/2022/04/cropped-favicon-32x32.webp
            [title] => 背字根
            [link] => https://www.beizigen.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.beizigen.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>';
	}
}

扩展阅读

fetch_rss()函数位于:wp-includes/rss.php

相关函数:

背字根微信二维码

本站文章均为原创,码字非常不容易,转载请注明原文出处,给苦逼的作者保留一点创作动力。