1

I'm trying to create a feed of the most recent 10 posts from 5 different feeds all combined into one list. I found a way to combine the feeds but have not been able to easily limit them to 10 and sort them by date.

Can someone point me in the right direction? Thanks!!!

<ul>
<?php $rsslist = array(
'http://www.nrel.gov/news/press/rss/rss.xml',
'http://www.pewenvironment.org/rss/campaigns-8589935316',
'http://www.ashrae.org/RssFeeds/ashrae.xml',
'http://www.districtenergy.org/blog/category/idea-activity/feed/',
'http://www.districtenergy.org/blog/category/industry-news/feed/'
 );

$feedlist = array();
foreach ( $rsslist as $rssurl ) $feedlist []= fetch_feed( $rssurl ); /* store in feed array for later access */

$current_item_cycle = 0;
while ( sizeof($feedlist) ) { /* while feedlist is not empty */ 

    foreach ( $feedlist as $index => $feed ) { /* cycle through each feed */

        if (!$item = $feed->get_item($current_item_cycle++)) unset($feedlist[$index]); /* if feed has not more items unset */

        else /* echo it out */ { ?>
            <li>
            <?php echo $item->get_date('j F Y | g:i a') . $item->get_permalink(); ?>
            </li>
        <?php }
    }
} ?>
</ul>
1

1 Answer 1

1

This code should work. I commented out one of your feeds. When I uncomment it, it stops sorting things correctly. It seems that there is something wrong with it. It doesn't get formatted in the browser correctly like the rest either.

<ul>
<?php 
$rsslist = array(
    #'http://www.pewenvironment.org/rss/campaigns-8589935316',
    'http://www.ashrae.org/RssFeeds/ashrae.xml',
    'http://www.districtenergy.org/blog/category/idea-activity/feed/',
    'http://www.districtenergy.org/blog/category/industry-news/feed/',
    'http://www.nrel.gov/news/press/rss/rss.xml'
);

// Fetch all of the feeds into a simplePie mashup
$rss = fetch_feed($rsslist);
// Set the max items to either 10 or all items if there are less than 10
$maxitems = $rss->get_item_quantity(10);

// Get the items (0-10)
$rss_items = $rss->get_items(0, $maxitems); 

// If there are no items, output that
if ($maxitems == 0) {
    echo '<li>No items.</li>';
// Otherwise loop over the items
} else {
    foreach ( $rss_items as $item ) { ?>
        <li>
        <?php echo $item->get_date() . $item->get_permalink(); ?>
        </li>
    <?php 
    }
}
?>
</ul>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.