combine multiple feeds with fetch_feed and display blog titles for each item?

I’m trying to display multiple feeds using the fetch_feed function. Its working well thus far except I can’t figure out why the title for each individual rss feed will not show up.
Here is my code:

<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/feed.php');
$rsslist = array(   'http://www.lt11.com/rss',
                    'http://feeds.feedburner.com/climbingnarc'
                );
$rss = fetch_feed($rsslist);
if (!is_wp_error( $rss ) ) : 
$maxitems = $rss->get_item_quantity(25); 
$rss_items = $rss->get_items(0, $maxitems); 
endif;
?>
    <?php if ($maxitems == 0) echo '<li>No items.</li>';
    else
    foreach ( $rss_items as $item ) : ?>
    <li class="feed"> 
        <a href='<?php echo esc_url( $item->get_permalink() ); ?>'
        title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
        <?php echo esc_html( $item->get_date('j/n/Y - g:i A') ); ?> - <div class="feeditemtitle"><?php echo esc_html( $item->get_title() ); ?></div> - <?php echo esc_html($rss->get_title() ); ?></a>
    </li>
    <?php endforeach; ?>        

Its that list bit that calls for the title of the rss feed. If I only fetch one feed then the title shows up. When I fetch more than one feed as in the example above, the title does not show up. Any ideas on how to fix this? Thanks!

Related posts

Leave a Reply

2 comments

  1. I don’t see anything to suggest that fetch_feed is supposed to accept an array of values. That is not mentioned in the Codex, nor the source, @param string $url URL to retrieve feed.

    That said, fetch_feed passes things off to SimplePie’s set_feed_url($url); which does accept an array. And that Simplepie docs page is, I think, where your answer is. The RSS feed name is $item->get_feed()->get_title() not $rss->get_title()

    I am still a bit cautious since you are passing an array through a WordPress function that doesn’t appear to be meant for it.

  2. Display above code with featured image …

    <div class="clearfix">
    <?php 
    
    include_once(ABSPATH . WPINC . '/feed.php');
    $rsslist = array(   'http://www.bloggingwizard.com/feed',
                        'http://www.wpbeginner.com/feed/'
                    );
    $rss = fetch_feed($rsslist);
    if (!is_wp_error( $rss ) ) : 
    $maxitems = $rss->get_item_quantity(5); 
    $rss_items = $rss->get_items(0, $maxitems); 
    endif;
    ?>
    <?php function get_first_image_url($html)
            {
                if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
                return $matches[1];
                }
            }
    ?>  
    
    
        <?php if ($maxitems == 0) echo '<li>No items.</li>';
        else
        foreach ( $rss_items as $item ) : ?>
        <div class="col-md-4"> 
    
        <div class="rss-image" style="background-image: url('<?php echo get_first_image_url($item->get_content()) ?>');height: 200px;
        background-size: cover;">
    
            </div>
    
            <a href='<?php echo esc_url( $item->get_permalink() ); ?>'
            title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
           <div class="feeditemtitle">
           <?php echo esc_html( $item->get_title() ); ?></div>  <?php echo esc_html($rss->get_title() ); ?></a>
        </div>
        <?php endforeach; ?>   
    
    </div>