^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数fetch_feed()用法示例

fetch_feed( mixed $url )

Build SimplePie object based on RSS or Atom feed from URL.


描述


参数

$url

(mixed)
(Required)
URL of feed to retrieve. If an array of URLs, the feeds are merged using SimplePie’s multifeed feature. See also ​<a href="http://simplepie.org/wiki/faq/typical_multifeed_gotchas">http://simplepie.org/wiki/faq/typical_multifeed_gotchas</a>

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包


返回值

(WP_Error|SimplePie) WP_Error object on failure or SimplePie object on success


源代码

File: wp-includes/feed.php

function fetch_feed( $url ) {
	if ( ! class_exists( 'SimplePie', false ) ) {
		require_once( ABSPATH . WPINC . '/class-simplepie.php' );
	}

	require_once( ABSPATH . WPINC . '/class-wp-feed-cache.php' );
	require_once( ABSPATH . WPINC . '/class-wp-feed-cache-transient.php' );
	require_once( ABSPATH . WPINC . '/class-wp-simplepie-file.php' );
	require_once( ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php' );

	$feed = new SimplePie();

	$feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
	// We must manually overwrite $feed->sanitize because SimplePie's
	// constructor sets it before we have a chance to set the sanitization class
	$feed->sanitize = new WP_SimplePie_Sanitize_KSES();

	$feed->set_cache_class( 'WP_Feed_Cache' );
	$feed->set_file_class( 'WP_SimplePie_File' );

	$feed->set_feed_url( $url );
	/** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */
	$feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
	/**
	 * Fires just before processing the SimplePie feed object.
	 *
	 * @since 3.0.0
	 *
	 * @param object &$feed SimplePie feed object, passed by reference.
	 * @param mixed  $url   URL of feed to retrieve. If an array of URLs, the feeds are merged.
	 */
	do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
	$feed->init();
	$feed->set_output_encoding( get_option( 'blog_charset' ) );

	if ( $feed->error() )
		return new WP_Error( 'simplepie-error', $feed->error() );

	return $feed;
}

更新日志

Version 描述
2.8.0 Introduced.

相关函数

Uses

  • wp-includes/class-wp-feed-cache-transient.php:
    wp_feed_cache_transient_lifetime
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/plugin.php:
    do_action_ref_array()
  • wp-includes/feed.php:
    wp_feed_options
  • wp-includes/option.php:
    get_option()
  • wp-includes/class-wp-error.php:
    WP_Error::__construct()
  • Show 1 more use


Used By

  • wp-admin/includes/dashboard.php:
    wp_dashboard_rss_control()
  • wp-admin/includes/deprecated.php:
    wp_dashboard_plugins_output()
  • wp-includes/widgets/class-wp-widget-rss.php:
    WP_Widget_RSS::widget()
  • wp-includes/widgets.php:
    wp_widget_rss_output()
  • wp-includes/widgets.php:
    wp_widget_rss_process()

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Example
    This example will retrieve and display a list of links for an existing RSS feed, limiting the selection to the five most recent items:

    
    <h2><?php _e( 'Recent news from Some-Other Blog:', 'wpdocs_textdomain' ); ?></h2>
    
    <?php // Get RSS Feed(s)
    include_once( ABSPATH . WPINC . '/feed.php' );
    
    // Get a SimplePie feed object from the specified feed 源代码.
    $rss = fetch_feed( 'http://example.com/rss/feed/goes/here' );
    
    $maxitems = 0;
    
    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;
    ?>
    
    <ul>
        <?php if ( $maxitems == 0 ) : ?>
            <li><?php _e( 'No items', 'wpdocs_textdomain' ); ?></li>
        <?php else : ?>
            <?php // Loop through each feed item and display each item as a hyperlink. ?>
            <?php foreach ( $rss_items as $item ) : ?>
                <li>
                    <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                        title="<?php printf( __( 'Posted %s', 'wpdocs_textdomain' ), $item->get_date('j F Y | g:i a') ); ?>">
                        <?php echo esc_html( $item->get_title() ); ?>
                    </a>
                </li>
            <?php endforeach; ?>
        <?php endif; ?>
    </ul>
    
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈