Some of us don’t notice it, however WordPress has built-in the power 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 power to publish different feeds immediately into your WordPress template.
WordPress helps each Magpie and SimplePie RSS Caching inside its out there perform, fetch_feed:
- fetch_feed – retrieve an RSS feed from a URL with computerized caching
This actually is useful in case you have 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 web site routinely as you publish your content material.
I’ve additionally utilized this strategy to publish podcasts and video feeds from one web site 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 );
$gadgets = array_slice($rss->get_items, 0, $maxitems);
endif;
?>
<ul>
<?php if (empty($gadgets)) echo '<li>No gadgets</li>';
else
foreach ( $gadgets 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 web site, take into account that fetch_feed caches for 12 hours by default. You may 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 length
add_filter( 'wp_feed_cache_transient_lifetime', 'update_cache_time');
When you’d wish to replace the cache for a particular 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 properly.