Some people don’t understand 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 need to embody the power to publish different feeds instantly into your WordPress template.
WordPress helps each Magpie and SimplePie RSS Caching inside its out there operate, fetch_feed:
- fetch_feed – retrieve an RSS feed from a URL with automated caching
This actually is useful you probably have a number of websites and need 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 strategy to publish podcasts and video feeds from one website to a different.
WordPress Theme or Little one 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 submit on one other website, needless to say fetch_feed caches for 12 hours by default. You possibly can modify this by modifying the time interval through the filter wp_feed_cache_transient_lifetime.
operate 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 prefer 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 can publish feeds for you as nicely.