Fetch several rss feeds from other blogs in one page

203 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

Much easier to use WordPress's built-in RSS function. See https://codex.wordpress.org/Function_Reference/fetch_feed

Use it as many times as you want in a php template, or make it generate a shortcode. Style the <ul> and <li> and add a containing <div> if needed.

Example:

<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'http://example.com/rss/feed/goes/here' );

$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( 5 ); 

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

endif;
?>

<ul>
    <?php if ( $maxitems == 0 ) : ?>
        <li><?php _e( 'No items', 'my-text-domain' ); ?></li>
    <?php else : ?>
        <?php // Loop through each feed item and display each item as a hyperlink. ?>
        <?php foreach ( $rss_items as $item ) : ?>
            <li>
                <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                    title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
                    <?php echo esc_html( $item->get_title() ); ?>
                </a>
            </li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>