A function to fetch blog content via rss feed

I am trying to make a function which take an rss fedd URL and fetches the most recent 2 posts. I have tried to remake the snippet from here to a full function in funtions.php as following. I don’t want to use a plugin for this since the plugins I have looked at have been close to impossible to style with my own html…

function fetch_feed_from_blogg($path) {
$rss = fetch_feed($path);

if (!is_wp_error( $rss ) ) : 

$maxitems = $rss->get_item_quantity(2); 
$rss_items = $rss->get_items(0, $maxitems); 
endif;

function get_first_image_url($html)
{
  if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
  return $matches[1];
  }
}

function shorten($string, $length) 
{
$suffix = '&hellip;';

$short_desc = trim(str_replace(array("/r", "/n", "/t"), ' ', strip_tags($string)));
    $desc = trim(substr($short_desc, 0, $length));
    $lastchar = substr($desc, -1, 1);
      if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?') $suffix='';
          $desc .= $suffix;
    return $desc;
}

if ($maxitems == 0) echo '<li>No items.</li>';
else 
foreach ( $rss_items as $item ) :

$html = '<ul class="rss-items" id="wow-feed"> <li class="item"> <span class="rss-image"><img src="' .get_first_image_url($item->get_content()). '"/></span>
    <span class="data"><h5><a href="' . esc_url( $item->get_permalink() ) . '" title="' . esc_html( $item->get_title() ) . '"' . esc_html( $item->get_title() ) . '</a></h5></li></ul>';

 return $html;
}

I am also trying to make it so that it can be used several times on a single page.

Solutions Collecting From Web of "A function to fetch blog content via rss feed"

First things first, avoid defining functions inside functions. PHP doesn’t have a problem with it, but it’s usually a sign you’re “doing it wrong” and it just leads to unnecessary headaches.

Second, your foreach loop at the bottom is a little malformed. You need to define the <ul> outside the loop, and then append to $html – otherwise you just end up with the last item!

function wpse_187819_get_feed_html( $url ) {
    if ( is_wp_error( $rss = fetch_feed( $url ) ) )
        return; // Bail

    $maxitems  = $rss->get_item_quantity( 2 );
    $rss_items = $rss->get_items( 0, $maxitems );

    $html = '<ul class="rss-items" id="wow-feed">';

    if ( $maxitems ) {
        foreach ( $rss_items as $item ) {
            $title = esc_attr( $item->get_title() );
            $link  = esc_url( $item->get_permalink() );

            $html .= '<li class="item">'; 
                if ( preg_match( '/<img.+?src="(.+?)"/', $item->get_content(), $matches ) )
                    $html .= '<span class="rss-image"><img src="' . $matches[1] . '"/></span>';

                $html .= '<span class="data"><h5><a href="' . $link . '" title="' . $title . '"' . $title . '</a></h5></span>';
            $html .= '</li>';
        }

    } else {
        $html .= '<li>No items</li>';
    }

    // All done, now close the <ul>
    $html .= '</ul>';

    return $html;
}