fetch_feed()

最后更新于:2021-11-26 04:05:49

fetch_feed( string|string[]$url)

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

参数

$url

(string|string[]) (Required) URL of feed to retrieve. If an array of URLs, the feeds are merged using SimplePie’s multifeed feature. See also http://simplepie.org/wiki/faq/typical_multifeed_gotchas

响应

(SimplePie|GC_Error) SimplePie object on success or GC_Error object on failure.

源文件

文件: gc-includes/feed.php

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

	require_once ABSPATH . GCINC . '/class-gc-feed-cache-transient.php';
	require_once ABSPATH . GCINC . '/class-gc-simplepie-file.php';
	require_once ABSPATH . GCINC . '/class-gc-simplepie-sanitize-kses.php';

	$feed = new SimplePie();

	$feed->set_sanitize_class( 'GC_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 GC_SimplePie_Sanitize_KSES();

	// Register the cache handler using the recommended method for SimplePie 1.3 or later.
	if ( method_exists( 'SimplePie_Cache', 'register' ) ) {
		SimplePie_Cache::register( 'gc_transient', 'GC_Feed_Cache_Transient' );
		$feed->set_cache_location( 'gc_transient' );
	} else {
		// Back-compat for SimplePie 1.2.x.
		require_once ABSPATH . GCINC . '/class-gc-feed-cache.php';
		$feed->set_cache_class( 'GC_Feed_Cache' );
	}

	$feed->set_file_class( 'GC_SimplePie_File' );

	$feed->set_feed_url( $url );
	/** This filter is documented in gc-includes/class-gc-feed-cache-transient.php */
	$feed->set_cache_duration( apply_filters( 'gc_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );

	/**
	 * Fires just before processing the SimplePie feed object.
	 *
	 * @since 3.0.0
	 *
	 * @param SimplePie       $feed SimplePie feed object (passed by reference).
	 * @param string|string[] $url  URL of feed or array of URLs of feeds to retrieve.
	 */
	do_action_ref_array( 'gc_feed_options', array( &$feed, $url ) );

	$feed->init();
	$feed->set_output_encoding( get_option( 'blog_charset' ) );

	if ( $feed->error() ) {
		return new GC_Error( 'simplepie-error', $feed->error() );
	}

	return $feed;
}
<h2><?php _e( 'Recent news from Some-Other Blog:', 'gcdocs_textdomain' ); ?></h2>

<?php // Get RSS Feed(s)
include_once( ABSPATH . GCINC . '/feed.php' );

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'http://example.com/rss/feed/goes/here' );

$maxitems = 0;

if ( ! is_gc_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', 'gcdocs_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="https://docs.gechiui.com/functions/fetch_feed/<?php echo esc_url( $item->get_permalink() ); ?>"
                    title="<?php printf( __( 'Posted %s', 'gcdocs_textdomain' ), $item->get_date('j F Y | g:i a') ); ?>">
                    <?php echo esc_html( $item->get_title() ); ?>
                </a>
            </li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>