Search
Close this search box.

RSS Aggregator Code To Pull RSS Feed Content On Your Page Manually

[updated_on]
Share this post:
<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'https://www.youtube.com/channel/UCZEUuBtKIvoZPSQMgtKUw5A' );
// $rss = fetch_feed( 'http://www.forbes.com/taxes-law/feed2/', 'http://www.wsj.com/xml/rss/3_7031.xml' );

/*
$rsslist = array(   'http://www.forbes.com/taxes-law/feed2/',
                    'http://www.wsj.com/xml/rss/3_7031.xml',
                    'http://www.taxlawroundup.com/?feed=rss'
                );
$rss = fetch_feed($rsslist);
*/

$maxitems = 0;

if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly

    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity( 7 ); 

    // Build an array of all the items, starting with element 0 (first element).
    $rss_items = $rss->get_items( 0, $maxitems );

endif;
?>


    <?php if ( $maxitems == 0 ) : ?>
<ul>
<li><?php _e( 'No items', 'my-text-domain' ); ?></li>
</ul>
    <?php else : ?>
        // Loop through each feed item and display each item as a hyperlink.
        <?php foreach ( $rss_items as $item ) : 

echo '<div class="rss-feed-container">';
if ($enclosure = $item->get_enclosure())
                            {   
                                echo '<img class="feed-thumb" style="float: left; width: 200px; margin-right: 10px;" src="' . $enclosure->get_thumbnail() . '" />';
                            }
?>
            
                <a title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>" href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank" rel="noopener">
                    <?php echo esc_html( $item->get_title() ); ?></a> - <?php echo esc_html( $item->get_date('F j, Y') ); ?>
<p><?php echo substr($item->get_description(), 0, 150); ?> <a title="<?php echo esc_html( $item->get_title() ); ?>" href="<?php echo esc_url( $item->get_permalink() ); ?>">Continue Reading</a></p>
<?php echo '</div>'; ?>
        <?php endforeach; ?>
    <?php endif; ?>
Share this post:

8 thoughts on “RSS Aggregator Code To Pull RSS Feed Content On Your Page Manually”

    1. Hi Raju,

      Do you mean creating an RSS feed for a custom post type or adding custom fields to an existing RSS feed?

      Depending on how you want to modify the feeds these two could be helpful. If you’re happy with adding the content of these feeds to the content area of the feed, then this will be helpful http://www.paulund.co.uk/add-custom-fields-to-rss-feed.

      If you want to modify the feed in some other way then I suggest you look at this https://codex.wordpress.org/Customizing_Feeds

      I hope that helps and thanks for stopping by!

  1. Hey, love your youtube channel! I’m trying to use your code in a custom template file however it doesn’t seem to be working. Is there any chance you could help me out a little with this? im working off of a dev domain as the site is not ready to launch as of yet.

    currently developing on: http://elmers.cornerstone-dev.co.uk/

    trying to pull in a news feed via: http://elmersbigparadesuffolk.co.uk/?cat=2&feed=rss2

    if you could shed some light i would be very grateful 🙂

    1. Hi Steve,

      I tested the code on the wp-phd.com demo site and the code below worked:

      get_item_quantity( 7 );

      // Build an array of all the items, starting with element 0 (first element).
      $rss_items = $rss->get_items( 0, $maxitems );

      endif;
      ?>

      <?php foreach ( $rss_items as $item ) :

      echo '

      ‘;
      if ($enclosure = $item->get_enclosure())
      {
      echo ‘get_thumbnail() . ‘” class=”feed-thumb” style=”float:left;width:200px;margin-right:10px;” />’;
      }
      ?>

      get_permalink() ); ?>” title=”get_date(‘j F Y | g:i a’) ); ?>” target=”_blank”>
      get_title() ); ?>
      – get_date(‘F j, Y’) ); ?>

      get_description(), 0, 150); ?>
      get_permalink() ); ?>” title=”get_title() ); ?>”>Continue Reading
      <?php echo '

      ‘; ?>

      I don’t know the exact reason it worked for me and not for you. The error on your development site says that the URL is invalid, have you double checked that you have the correct URL there?

Leave a Comment

Your email address will not be published. Required fields are marked *

WPLearningLab