函数参考/fetch feed

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn

描述

Retrieves an external feed and parses it. Uses the SimplePie and FeedCache functionality for retrieval and parsing and automatic caching. 使用SimplePie获取RSS供稿并解析内容

用法

%%%<?php $feed = fetch_feed( $uri ); ?>%%%

参数

tt$uri/tt
(URI) (required) 你想要获取内容的RSS的URI。函数将返回一个标准的SimplePie对象。
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

示例

This example will retrieve and display a list of links for an existing RSS feed, limiting the selection to the five most recent items: 本示例获取并显示一个RSS 5个最新的项目: %%%

<?php _e('Recent news from Some-Other Blog:'); ?>

<?php // 获取RSS种子(可为多个) 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'); 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; ?>

    <?php if ($maxitems == 0) echo '
  • No items.
  • ';
       else
       // Loop through each feed item and display each item as a hyperlink.
       foreach ( $rss_items as $item ) : ?>
    
  • <a href='<?php echo $item->get_permalink(); ?>' title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'> <?php echo $item->get_title(); ?></a>
  •    <?php endforeach; ?>
    

%%%

相关资源