Wednesday, November 15, 2023
HomeMobile MarketingHow To Syndicate Exterior RSS Feeds In Your WordPress Theme or Baby...

How To Syndicate Exterior RSS Feeds In Your WordPress Theme or Baby Theme


Some people 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 straight 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 computerized caching

This actually turns out to be useful you probably have a number of websites and wish to share your weblog posts on the opposite websites as quickly as they publish. It can 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 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>

In the event you publish and don’t instantly see your new put up on one other website, needless to say 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.

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');

In the event you’d prefer to replace the cache for a selected feed, you may 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 nicely.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments