Display RSS feeds in WordPress

If you have multiple WordPress websites, you might want to show titles and excerpts of latest posts of one site on another. You can also use the RSS (Real Simple Syndication) feeds of other websites and show them on your own WordPress site. Use the code snippet blow to show RSS feed of any website in your WordPress blog.

Copy the code, replace http://example.com/feed/ with your target RSS feed link and change the number of RSS items to be displayed. Paste the code anywhere in your WordPress theme template. You can use it in sidebar.php, footer.php or just any template. Make sure to take care of the PHP opening and closing tags. (I assume you are familiar with the basic PHP syntax.)

<?php
include_once(ABSPATH . WPINC . '/rss.php');
$feed = 'http://example.com/feed/'; //URL of your target feed
$rss = fetch_feed($feed);
if (!is_wp_error( $rss ) ) :
    $maxitems = $rss->get_item_quantity(3); //Number of feed items to be displayed
    $rss_items = $rss->get_items(0, $maxitems);
    if ($rss_items):
        echo "<ul>\n";
        foreach ( $rss_items as $item ) :
            echo '<li>';
            echo '<a href="' . $item->get_permalink() . '">' . $item->get_title() . "</a>\n";
            echo '<p>' . $item->get_description() . "</li>\n";
        endforeach;
        echo "</ul>\n";
    endif;
endif;
?>