是的,您的代码确实会比正常代码消耗更多的资源。因为您正在使用feed和fetch_feed()
消费更多。它基于RSS或来自URL的Atom提要创建SimplePie对象。你可以做一件事,如下限制你的帖子:
<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 source.
$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>