Wednesday, January 4, 2023
HomeMobile MarketingHow To Syndicate Exterior RSS Feeds In Your WordPress Theme or Little...

How To Syndicate Exterior RSS Feeds In Your WordPress Theme or Little one Theme


Some people don’t understand 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 need to embrace the flexibility to publish different feeds straight into your WordPress template.

WordPress helps each Magpie and SimplePie RSS Caching inside its accessible operate, fetch_feed:

  • fetch_feed – retrieve an RSS feed from a URL with computerized caching

This actually turns out to be 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 search engine optimization standpoint, producing backlinks on one other website routinely 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>

In case 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’ll be able to 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 length
add_filter( 'wp_feed_cache_transient_lifetime', 'update_cache_time');

In case you’d prefer 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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments