Some of us don’t notice it, however WordPress has built-in the flexibility to syndicate RSS feeds with some out-of-the-box options. Whereas there are widgets to do that, you may very well wish to embrace the flexibility to publish different feeds straight into your WordPress template.
WordPress helps each Magpie and SimplePie RSS Caching inside its obtainable perform, fetch_feed:
- fetch_feed – retrieve an RSS feed from a URL with computerized caching
This actually turns out to be useful when you’ve got a number of websites and wish to share your weblog posts on the opposite websites as quickly as they publish. It may also be good from an website positioning standpoint, producing backlinks on one other website robotically as you publish your content material.
I’ve additionally utilized this method to publish podcasts and video feeds from one website to a different.
WordPress Theme or Baby Theme Template
// Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );
$rss = fetch_feed('https://feed.martech.zone');
if ( ! is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity( 5 );
$objects = array_slice($rss->get_items, 0, $maxitems);
endif;
?>
<ul>
<?php if (empty($objects)) echo '<li>No objects</li>';
else
foreach ( $objects as $merchandise ) : ?>
<li><a href='<?php echo esc_url( $item->get_permalink() ); ?>'
title='<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>'>
<?php echo esc_html( $item->get_title() ); ?>
</a></li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
When you publish and don’t instantly see your new put up on one other website, remember the fact that fetch_feed caches for 12 hours by default. You’ll be able to modify this by modifying the time interval by way of the filter wp_feed_cache_transient_lifetime.
perform update_cache_time( $seconds )
{
// change the default feed cache recreation interval to 1 hour
return (int) 3600;
}
//set feed cache period
add_filter( 'wp_feed_cache_transient_lifetime', 'update_cache_time');
When you’d wish to replace the cache for a selected feed, you’ll be able to apply the filter, fetch the feed, after which reapply the default cache time by updating your code as follows:
// filter to set cache lifetime
add_filter( 'wp_feed_cache_transient_lifetime' , 'update_cache_time' );
$rss = fetch_feed( $feed_url );
// reset the cache lifetime to default worth
remove_filter( 'wp_feed_cache_transient_lifetime' , 'update_cache_time' );
Edit your WordPress template (Design > Theme Editor) and place the code the place you’d just like the feed to publish. There are additionally a ton of sidebar widgets on the market that may publish feeds for you as effectively.